【问题标题】:Angular2 Routing Error: Uncaught (in promise): TypeError: Unable to get property 'visitExpression'Angular2路由错误:未捕获(承诺):TypeError:无法获取属性'visitExpression'
【发布时间】:2016-12-09 15:54:18
【问题描述】:

我已经为这个错误苦苦挣扎了一段时间了。经过广泛的研究,我找不到任何其他参考。任何帮助或方向将不胜感激。我相信这个问题与我如何使用路由和 auth0 服务相结合。请注意,我的路由都没有显示,但我的导航栏等会显示。

编辑: 最初我假设 Auth0 与这个问题有关。我已经从应用程序中删除了所有 Auth0 组件、服务和导入。因此,无论出于何种原因,路由根本无法正常工作....我已经更新了发布的代码,以便更容易挑选。

错误

 Error: Uncaught (in promise): TypeError: Unable to get property 'visitExpression' of undefined or null reference
(http://localhost:3000/node_modules/zone.js/dist/zone.js:538:26) at  Anonymous function 

main.ts

import { bootstrap }    from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';    
import { appRouterProviders } from './app.routes';

bootstrap(AppComponent, [ appRouterProviders]);

app.component.ts

import { Component } from '@angular/core';
import { HomePageHeaderComponent } from './homepageheader.component';    
import { ROUTER_DIRECTIVES, Router } from '@angular/router';

@Component({
selector: 'my-app',
template: `<header></header><router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES],


})
export class AppComponent {
}

**app.routes.ts*

import { provideRouter, RouterConfig } from '@angular/router';
import { HomePageContentComponent } from './homepagecontent.component';
import { LoggedInComponent } from './loggedin.component';
import { LogoutComponent } from './logout.component';

const routes: RouterConfig = [

{ path: '', pathMatch: 'full', redirectTo: 'home' },
{ path: 'home', component: HomePageContentComponent },
{ path: 'loggedIn', component: LoggedInComponent },
{ path: 'loggedOut', component: LogoutComponent },

];

export const appRouterProviders = [
provideRouter(routes)
];

Systemjs.config

(function (global) {
// map tells the System loader where to look for things
var map = {
    'app': 'app/components', //'dist',
    '@angular': 'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs': 'node_modules/rxjs',
    'angular2-jwt': 'node_modules/angular2-jwt/angular2-jwt.js',
};
// packages tells the System loader how to load when no filename and/or no extension
var packages = {
    'app': { main: 'main.js', defaultExtension: 'js' },
    'rxjs': { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: 'index.js', defaultExtension: 'js' },



};
var ngPackageNames = [
  'common',
  'compiler',
  'core',
  'forms',
  'http',
  'platform-browser',
  'platform-browser-dynamic',
  'router',
  'router-deprecated',
  'upgrade',
];
// Individual files (~300 requests):
function packIndex(pkgName) {
    packages['@angular/' + pkgName] = { main: 'index.js', defaultExtension: 'js' };
}
// Bundled (~40 requests):
function packUmd(pkgName) {
    packages['@angular/' + pkgName] = { main: '/bundles/' + pkgName + '.umd.js', defaultExtension: 'js' };
}
// Most environments should use UMD; some (Karma) need the individual index files
var setPackageConfig = System.packageWithIndex ? packIndex : packUmd;
// Add package entries for angular packages
ngPackageNames.forEach(setPackageConfig);
var config = {
    map: map,
    packages: packages
};
System.config(config);
})(this);

package.json

{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"license": "ISC",
"dependencies": {
 "@angular/common": "2.0.0-rc.4",
 "@angular/compiler": "2.0.0-rc.4",
 "@angular/core": "2.0.0-rc.4",
 "@angular/forms": "0.2.0",
 "@angular/http": "2.0.0-rc.4",
 "@angular/platform-browser": "2.0.0-rc.4",
 "@angular/platform-browser-dynamic": "2.0.0-rc.4",
 "@angular/router": "3.0.0-beta.1",
 "angular/router-deprecated": "2.0.0-rc.2",
"angular/upgrade": "2.0.0-rc.4",
"angular2-in-memory-web-api": "0.0.14",
"bootstrap": "^3.3.7",
"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"systemjs": "0.19.27",
"zone.js": "^0.6.12"
},
"devDependencies": {
"concurrently": "^2.0.0",
"lite-server": "^2.2.0",
"typescript": "^1.8.10",
"typings": "^1.3.2"
}
}

【问题讨论】:

  • 我认为第一条路线应该有 pathMatch: 'full' { path: '', component: HomePageContentComponent, pathMatch: 'full' }, 但不知道这是否能解决您的问题。我没有发现任何明显的错误。

标签: angular angular2-routing


【解决方案1】:

发现了一些明显的东西 ;-)

不要从

导入
import { ROUTER_PROVIDERS } from '@angular/router-deprecated';

改用

import { ROUTER_PROVIDERS } from '@angular/router';

您可能还需要修复您的 systemjs 配置。

【讨论】:

  • 感谢您抽出宝贵时间对此进行调查。我已经更改了您提到的所有内容,但在编辑过程中忘记了!我还将在编辑中添加我的 systemjs.config。但是改变这几件事没有用:/
  • 你能在 Plunker 中复制吗?
  • 不,我已经尝试过,但 plunker 在加载我的项目之前就死了。我将继续尝试将我的项目缩减为 plunker 的核心组件
  • 精简到最小的 repo 将是创建 Plunker 的主要任务。请不要指望任何人调查包含大量代码的 Plunker。
  • 使用 plunker 运气不佳,它给了我 URL 被阻止的错误。我假设它是因为我工作的安全限制。我可能要等到我回家。但是我确实使用我的 config 和 package.json 文件进行了编辑。
【解决方案2】:

我发现了问题!在我试图路由到的其他组件中,我有空的选择器标签。例如这里是我的路径

{ path: '', pathMatch: 'full', redirectTo: '/home' },
{ path: 'home', component: HomePageContentComponent },
{ path: 'loggedIn', component: LoggedInComponent },
{ path: 'loggedOut', component: LogoutComponent },

在我的 LoggedInComponent 和 LogoutComponent 中

selector:'',

这导致了“访问表达式”错误。因此,只需从组件中删除它们即可解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多