在不同模块中使用 angular 组件路由
Using angular component routing in different modules
为什么模块 2 显示在模块 1 中?
我们有一个 angular 应用程序包含多个模块。
对于某些模块,我们希望使用组件路由器。
我们不知道该组件将加载到哪个页面 (url)。
现状:
在模块 1 中,正在显示模块 2 的模板。
想要的结果:
具有相对于组件的路径,以便
'...com/someUrlWhereModule1Lives#/step2' 加载模块 1
的第 2 步
'...com/someUrlWhereModule2Lives#/step2' 加载模块 2
的第 2 步
module1.module.js
angular
.module('app.module1', ['ngComponentRouter'])
.value('$routerRootComponent', 'module1')
.component('module1', {
templateUrl: 'module1.overview.html',
$routeConfig: [
{path: '/', name: 'Overview', component: 'module1Step1', useAsDefault: true},
{path: '/step2', name: 'Overview', component: 'module1Step2'}
]
})
.component('module1Step1', {
bindings: { $router: '<' },
templateUrl: 'module1.step1.html'
})
.component('module1Step2', {
bindings: { $router: '<' },
templateUrl: 'module1.step2.html'
});
module2.module.js
angular
.module('app.module2', ['ngComponentRouter'])
.value('$routerRootComponent', 'module2')
.component('module2', {
templateUrl: 'module2.overview.html',
$routeConfig: [
{path: '/', name: 'Overview', component: 'module2Step1', useAsDefault: true},
{path: '/step2', name: 'Step2', component: 'module2Step2'}
]
})
.component('module2Step1', {
bindings: { $router: '<' },
templateUrl: 'module2.step1.html'
})
.component('module2Step2', {
bindings: { $router: '<' },
templateUrl: 'module2.step2.html'
});
如果需要更多信息,请告诉我。
由于您将 Angular 应用程序用作单个寻呼机,因此我做了一些解决方法,您的模块(例如模块一)启动 $routerRootComponent(例如组件路由器组件)保存模块的路由。 $routerRootComponent 启动另一个组件(例如 module-one-overview),它有自己的 $routeConfig。
查看我的 Plunker 代码示例。
(抱歉,我不得不缩短 URL 以绕过 Whosebug 功能,该功能在引用 Plunker 时需要代码 URL)
为什么模块 2 显示在模块 1 中?
我们有一个 angular 应用程序包含多个模块。 对于某些模块,我们希望使用组件路由器。 我们不知道该组件将加载到哪个页面 (url)。
现状: 在模块 1 中,正在显示模块 2 的模板。
想要的结果:
具有相对于组件的路径,以便
'...com/someUrlWhereModule1Lives#/step2' 加载模块 1
的第 2 步
'...com/someUrlWhereModule2Lives#/step2' 加载模块 2
module1.module.js
angular
.module('app.module1', ['ngComponentRouter'])
.value('$routerRootComponent', 'module1')
.component('module1', {
templateUrl: 'module1.overview.html',
$routeConfig: [
{path: '/', name: 'Overview', component: 'module1Step1', useAsDefault: true},
{path: '/step2', name: 'Overview', component: 'module1Step2'}
]
})
.component('module1Step1', {
bindings: { $router: '<' },
templateUrl: 'module1.step1.html'
})
.component('module1Step2', {
bindings: { $router: '<' },
templateUrl: 'module1.step2.html'
});
module2.module.js
angular
.module('app.module2', ['ngComponentRouter'])
.value('$routerRootComponent', 'module2')
.component('module2', {
templateUrl: 'module2.overview.html',
$routeConfig: [
{path: '/', name: 'Overview', component: 'module2Step1', useAsDefault: true},
{path: '/step2', name: 'Step2', component: 'module2Step2'}
]
})
.component('module2Step1', {
bindings: { $router: '<' },
templateUrl: 'module2.step1.html'
})
.component('module2Step2', {
bindings: { $router: '<' },
templateUrl: 'module2.step2.html'
});
如果需要更多信息,请告诉我。
由于您将 Angular 应用程序用作单个寻呼机,因此我做了一些解决方法,您的模块(例如模块一)启动 $routerRootComponent(例如组件路由器组件)保存模块的路由。 $routerRootComponent 启动另一个组件(例如 module-one-overview),它有自己的 $routeConfig。
查看我的 Plunker 代码示例。
(抱歉,我不得不缩短 URL 以绕过 Whosebug 功能,该功能在引用 Plunker 时需要代码 URL)