Anuglar2 -找不到模块错误

Anuglar2 - Error cannot find module

本文关键字:错误 模块 找不到 Anuglar2      更新时间:2023-09-26

我使用Angular2 rc4目前我可以导入一个css文件与ExtractTextPlugin。我使用html-loader没有任何问题。然而,当我尝试加载css文件时,我得到错误:找不到模块"./app.component.css"预期的'styles'是字符串数组。我怎么解决这个问题?app.comonent.ts

import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES} from '@angular/router';
const template = require('./app.component.html'); 
const sytles = require('./app.component.css'); 
// Components
import {NavComponent} from './nav/nav.component';
import {FooterComponent} from './footer/footer.component';
@Component({
    selector: 'my-app',
    template: template,
    directives: [ROUTER_DIRECTIVES, NavComponent, FooterComponent],
    styles: [sytles],
    precompile: [NavComponent, FooterComponent]
})
export class AppComponent {
}

webpack.config.js

var webpack = require("webpack");
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
  entry: {
    "vendor": "./src/vendor",
    "app": "./src/boot"
  },
  output: {
    path: __dirname,
    filename: "./dist/[name].bundle.js"
  },
  resolve: {
    extensions: ['', '.ts', '.js']
  },
  devtool: 'source-map',
  module: {
    loaders: [
      {
        test: /'.ts/,
        loaders: ['ts-loader'],
        exclude: /node_modules/
      },
      {
          test: /'.html$/,
          loader: 'html'
      },
      {
          test: /'.css$/,
          loader: ExtractTextPlugin.extract("style-loader", "css-loader")
      }
    ]
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin(/* chunkName= */"vendor", /* filename= */"./dist/vendor.bundle.js"),
new ExtractTextPlugin('[name].css')
  ]
}

is

styles:[sytles];

应该是

styles:[styles];,

使用import而不是require,然后它会为你和模板创建一个单独的app.css文件,你需要angular2-template-loader,然后你不需要在模板上使用require,它会为你编译。

注意:-当你在css上导入时,你不需要将它添加到组件样式中,你可以直接在html中使用它。例如,当你使用bootstrap时,你不想把它添加到每个组件的每个样式中,但想只加载一次,那么你可以简单地在apps .ts中,这是入口点import "bootstrap",你可以在任何地方使用它。

webppack

{test: /'.ts$/, loaders: ["ts-loader","angular2-template-loader"]}

组件
templateUrl :"path"