2017-01-31 100 views
1

即時通訊使用webpack 2.2.0。我的AppModule正確加載,並在路線塊被下載,但它引發的模板語法錯誤angular 2 webpack 2延遲加載模塊拋出模板解析錯誤

「不能綁定到‘ngIf’,因爲它不是一個已知的財產」

如果我嘗試加載commonmodule在動態模塊,它拋出重複錯誤

動態路由(應用程序的路由):

{ 
    path: "reports/:id", 
    loadChildren:() => { 
     return System.import('./reports/tabularReportsFachade/tabularReportsFachade.module').then((comp: any) => { 
      debugger 
      return comp.TabularReportsFachadeModule; 
     }); 
    } 

孩子航線:

const routes: Routes = [ 
{ 
    path: "", 
    component: TabularReportsFachadeComponent,pathMatch: 'full' 
} 
]; 
export const routing: ModuleWithProviders = RouterModule.forChild(routes); 

兒童模塊:

@NgModule({ 
imports: [ routing, FormsModule], 
exports: [MyComponents], 
declarations: [MyComponents], 
bootstrap: [TabularReportsFachadeComponent], 

}) 
export class TabularReportsFachadeModule { 

static routing = routing; 
} 

webpack.config

var path = require('path'); 

module.exports = { 
entry: { 
    'polyfills': './Scripts/polyfills', 
    "vendor": "./Scripts/vendor", 
    "main": "./Scripts/main" 
}, 
output: { 
    filename: "./Scripts/[name].bundle.js", 
    path: __dirname, 
    publicPath: '/' 
}, 
resolve: { 
    extensions: [".ts", ".js", ".html"] 
}, 
devtool: 'sourcemap', 
module: { 
    loaders: [ 
     { 
      test: /.ts$/, 
      loader: 'babel-loader!ts-loader' 
     }, 
     { 
      test: /.css$/, 
      loader: 'style-loader!css-loader!sass-loader' 
     } 
    ] 
} 

};

回答

1

您需要添加BrowserModule了。

將BrowserModule導入根模塊中的CommonModule和其他模塊中要使用常用指令的CommonModule。

@NgModule({ 
    imports: [BrowserModule], 
    ... 
}) 
class AppModule {} 

@NgModule({ 
    imports: [CommonModule], 
    // Now MyComponent has access to ngIf 
    declarations: [MyComponent] 
    ... 
}) 
class OtherModule {} 

BrowserModule出口CommonModule,這樣就沒有必要直接在根模塊中導入CommonModule。

+0

感謝一噸。完美的作品! – janIreal23

+0

很酷。你也可以接受這個答案。 –

1

吳模塊添加CommonModule使用一些常用的指令:

@NgModule({ 
    imports: [ routing, FormsModule, CommonModule ], 
    exports: [MyComponents], 
    declarations: [MyComponents], 
    bootstrap: [TabularReportsFachadeComponent], 

}) 
export class TabularReportsFachadeModule { 

    static routing = routing; 
} 
+0

因爲我進口commonModule我appmodul裏面已經,進口在這裏再次給我複製模塊錯誤 – janIreal23

+0

我不這麼認爲,也許你已經進口BrowserModule中的AppModule,則導入CommonModule也。請注意,BrowserModule包含CommonModule。如果您已經擁有BrowserModule,請刪除CommonModule。但在其他模塊中,如果要使用像ngIf,NgFor等指令,則需要導入CommonModule或BrowserModule,而不是兩者。 –