我正在使用angular2路由.当用户在搜索字段中重新输入值并单击搜索时,需要重新加载相同的组件,但具有不同的路由参数.
<button (click)="onReload()">Search</button>
onReload() {
this.router.navigate(['results',this.location]);
}
Run Code Online (Sandbox Code Playgroud)
这是我的ResultsComponent的路径路径
{ path:'results/:location', component:ResultsComponent}
Run Code Online (Sandbox Code Playgroud)
此函数更改URL,但不会重新初始化组件.
在angularjs中,ui-router我可以像这样实现它.
$state.go($state.current, location, {reload: true});
Run Code Online (Sandbox Code Playgroud)
我如何在angular4中执行此操作?
当我在` http:// localhost/overview '时,模块沿着这条路径加载:
http://localhost/app/modules/overview/overview.module.js
但是当我转向页面http://localhost/overview/users然后按F5(刷新页面)时,我收到错误:
Error: Unexpected token <
Evaluating http://localhost/overview/app/modules/overview/overview.module
Run Code Online (Sandbox Code Playgroud)
发生错误是因为URL不正确,他应该是这样http://localhost/app/modules/overview/overview.module.
如何让它正常工作?
这是项目结构:
这是systemjs tsconfig:
{
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true
}
}
Run Code Online (Sandbox Code Playgroud)
这是systemjs配置:
(function (global) {
System.config({
baseURL: "/",
paths: {
'npm:': '/node_modules/'
},
map: {
app: 'app',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/forms': …Run Code Online (Sandbox Code Playgroud) 我目前的配置:
const routes: Routes = [
{ path: '', component: NavComponent, outlet: 'nav' }, // (1)
{ path: '**', component: NavComponent, outlet: 'nav' } // (2)
];
Run Code Online (Sandbox Code Playgroud)
有用.NavComponent总是呈现出口nav.特别是,它适用于以下所有类型的URL:
http://example.com/foo(nav:bar) // (a) non-empty path in nav --> (2)
http://example.com/foo(nav:) // (b) empty path in nav --> (2)
http://example.com/foo // (c) no nav at all --> (1)
Run Code Online (Sandbox Code Playgroud)
请注意,路由器匹配到这些URL的不同路由:
(1) 是用来 (c)(2)用于(a)和(b)这就是为什么NavComponent实例被破坏并重新创建每个位置的变化,从说时间(c)到(a).这是我需要防止的事情.我需要保留我的实例,因为它的状态,动画等等.据我所知,只有在所有URL使用相同的路由时才有可能,但是我找不到这样做的方法.如果我删除(1),喜欢的URL (c)停止显示NavComponent …
环境
我的角应用程序中有一个很大的组件树,有多个路径出口,可以显示每个级别上的特定组件.最深层次是管理某些信息的模式.
问题
如果你可以看到它(父组件),我可以阻止通过鼠标从我的子组件到父组件事件的交互,但是当我使用键盘时,我能够导航到父组件并在我的所有父组件中选择选项零件
这个问题
我该如何防止这种行为?
我正在研究 Angular-6 项目。我的应用程序有很多子路由。其中之一如下:
const routes: Routes = [
{
path: 'innovation-track/:innovation-track-id', component: InnovationTrackComponent,
children: [
{
path: 'sprint', component: ScrumBoardComponent
}
]
}
];
Run Code Online (Sandbox Code Playgroud)
我想从我的ScrumBoardComponent 中的URL获取创新轨道 ID值。
我知道我可以通过执行以下操作来做到这一点:
constructor(private _route: ActivatedRoute) { }
//somewhere in method
this._route.snapshot.parent.paramMap
Run Code Online (Sandbox Code Playgroud)
但是,我想在任何组件中获取创新轨道 ID值,无论它是子组件还是父组件都没有关系。我的意思是我不想做以下事情:
this._route.snapshot.parent.paramMap
Run Code Online (Sandbox Code Playgroud)
相反,我想做以下事情:
this._route.params.<any parameter name>
Run Code Online (Sandbox Code Playgroud) 我的问题是我有一个lang.component装载<router-outlet>.我需要的是访问根组件中的ActivatedRoute参数,app.component但是所有路由数据都是空的.我可以lang.component通过订阅params链接到你可以运行的代码来正常访问路由数据:https://plnkr.co/edit/gTXPUb6TvatbMyOPPRKj
如果您运行代码并打开控制台,则可以看到两个日志.它会告诉你问题所在.
请帮忙.
谢谢.
我正在使用Angular 4,这是我需要捕获该路由已更改并重新加载页面的组件之一中的代码。
ngOnInit() {
this.router.events.subscribe((value) => {
if (value instanceof NavigationEnd) {
console.log(value);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我在一条路线上遇到多个“ NavigationEnd”事件。对于某些路由console.log,甚至可以写6,7个值。
那怎么可能发生?
我的情况:
我有一个组件显示tile,每个tile表示一个数组中的对象,该数组通过ngfor循环.单击一个图块时,我想将该对象传递给另一个组件,该组件负责在可以修改的字段中显示该对象的所有属性.
我尝试过的:
在做了一些研究并发现多个帖子向我展示了如何为父子层次结构实现这一点以及一些帖子解释为了实现想要的功能需要使用共享服务,我决定尝试设置这样的服务.
然而,我似乎没有得到的是当我应该导航到不同的路线.似乎导航在早期找到了位置,因为在详细信息组件中检索它时,传递给服务的对象是未定义的.
我的代码:
显示切片的组件具有以下功能,可将单击的对象传递给共享服务:
editPropertyDetails(property: Property) {
console.log('Edit property called');
return new Promise(resolve => {
this.sharedPropertyService.setPropertyToDisplay(property);
resolve();
}).then(
() => this.router.navigate(['/properties/detail'])
)
}
Run Code Online (Sandbox Code Playgroud)
共享服务具有设置属性对象的功能和检索它的功能,如下所示:
@Injectable()
export class SharedPropertyService {
// Observable
public propertyToDisplay = new Subject<Property>();
constructor( private router: Router) {}
setPropertyToDisplay(property: Property) {
console.log('setPropertyToDisplay called');
this.propertyToDisplay.next(property);
}
getPropertyToDisplay(): Observable<Property> {
console.log('getPropertyToDisplay called');
return this.propertyToDisplay.asObservable();
}
}
Run Code Online (Sandbox Code Playgroud)
最后是必须接收被单击的对象但获取未定义对象的detail组件:
export class PropertyDetailComponent implements OnDestroy {
property: Property;
subscription: Subscription;
constructor(private sharedPropertyService: SharedPropertyService) {
this.subscription = this.sharedPropertyService.getPropertyToDisplay()
.subscribe(
property …Run Code Online (Sandbox Code Playgroud) 我知道这是一个常见的问题,但实际上我没有找到满足我需求的任何答案......
我有一个angular5像这样的路由配置的应用程序:
const routes: Routes = [
{ path: 'businessaccountmanagement', component:BusinessAccountManagementComponent },
...
];
Run Code Online (Sandbox Code Playgroud)
根据UrlSerializer文件我已经DefaultUrlSerializer像这样扩展:
import { DefaultUrlSerializer, UrlTree, UrlSerializer } from '@angular/router';
export class LowerCaseUrlSerializer extends DefaultUrlSerializer {
parse(url: string): UrlTree {
return super.parse(url.toLowerCase());
}
}
export const LowerCaseUrlSerializerProvider = {
provide: UrlSerializer,
useClass: LowerCaseUrlSerializer
};
Run Code Online (Sandbox Code Playgroud)
所以在这个实现中一切都工作得很好因为所有url案例将转换为小写并匹配 path: 'businessaccountmanagement'
但是,我需要的是宣布我的路线是upper camel case这样的:
const routes: Routes = [
{ path: 'BusinessAccountManagement', component:BusinessAccountManagementComponent },
...
];
Run Code Online (Sandbox Code Playgroud)
并接受所有匹配字符串的路由,无论字母大小写
接受的路线示例:
我们在Kubernetes集群上托管了基于Angular的Web应用程序.此应用程序的Ingress配置为添加基本URL:
{
"kind": "Ingress",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "test-app",
"namespace": "acceptance-testing",
...
"annotations": {
"kubernetes.io/ingress.class": "nginx",
"nginx.ingress.kubernetes.io/add-base-url": "true",
"nginx.ingress.kubernetes.io/rewrite-target": "/",
"nginx.ingress.kubernetes.io/ssl-redirect": "true"
}
},
"spec": {
"rules": [
{
"http": {
"paths": [
{
"path": "/at/test-app",
"backend": {
"serviceName": "test-app",
"servicePort": 80
}
}
]
}
}
]
},
...
}
Run Code Online (Sandbox Code Playgroud)
当我们在浏览器中输入包含客户端路由部分的URL时,ingress会将整个URL添加为在我们的场景中不正确的基础.
例如,对于https:// server/at/test-app/some-page请求基本URL应为https:// server/at/test-app /但我们接收https:// server/at/test-app /一些页/
我们已经切换到Angular哈希路由位置策略,现在它正常工作但我们想知道是否有某种方法使位置路由策略与nginx入口一起工作?
预先感谢您的帮助.
最好的祝福