我试图在home.componenet.ts
中使用多个CSS文件,但每次都会出错。请帮我解决这个问题。
我尝试使用两种方法,但是使用这两种方法,我都会遇到错误。
在第一种方法中,我在styleUrls中使用多个CSS文件,在第二种方法中,我通过导入多个CSS文件在app.component.css
中使用多个CSS文件。在第一种方法中,CSS插件文件夹位于 ClientApp->下。 app - >资产,在第二种方法中,CSS插件文件夹位于 wwwroot - >下dist - >资产
第一种方法:
以下是我的代码
import { Component } from '@angular/core';
import * as $ from 'jquery';
@Component({
selector: 'home',
templateUrl: './home.component.html',
styleUrls: ['~/app/assets/css/style.css','~/app/assets/css/colors/green.css']
})
export class HomeComponent {
public name = 'gfggfg';
}
-- package.json
{
"name": "iSol",
"private": true,
"version": "0.0.0",
"scripts": {
"test": "karma start ClientApp/test/karma.conf.js"
},
"devDependencies": {
"@angular/animations": "4.2.5",
"@angular/common": "4.2.5",
"@angular/compiler": "4.2.5",
"@angular/compiler-cli": "4.2.5",
"@angular/core": "4.2.5",
"@angular/forms": "4.2.5",
"@angular/http": "4.2.5",
"@angular/platform-browser": "4.2.5",
"@angular/platform-browser-dynamic": "4.2.5",
"@angular/platform-server": "4.2.5",
"@angular/router": "4.2.5",
"@ngtools/webpack": "1.5.0",
"@types/chai": "4.0.1",
"@types/jasmine": "2.5.53",
"@types/webpack-env": "1.13.0",
"angular2-router-loader": "0.3.5",
"angular2-template-loader": "0.6.2",
"aspnet-prerendering": "^3.0.1",
"aspnet-webpack": "^2.0.1",
"awesome-typescript-loader": "3.2.1",
"bootstrap": "3.3.7",
"chai": "4.0.2",
"css": "2.2.1",
"css-loader": "0.28.4",
"es6-shim": "0.35.3",
"event-source-polyfill": "0.0.9",
"expose-loader": "0.7.3",
"extract-text-webpack-plugin": "2.1.2",
"file-loader": "0.11.2",
"html-loader": "0.4.5",
"isomorphic-fetch": "2.2.1",
"jasmine-core": "2.6.4",
"jquery": "3.2.1",
"json-loader": "0.5.4",
"karma": "1.7.0",
"karma-chai": "0.1.0",
"karma-chrome-launcher": "2.2.0",
"karma-cli": "1.0.1",
"karma-jasmine": "1.1.0",
"karma-webpack": "2.0.3",
"preboot": "4.5.2",
"raw-loader": "0.5.1",
"reflect-metadata": "0.1.10",
"rxjs": "5.4.2",
"style-loader": "0.18.2",
"to-string-loader": "1.1.5",
"typescript": "2.4.1",
"url-loader": "0.5.9",
"webpack": "2.5.1",
"webpack-hot-middleware": "2.18.2",
"webpack-merge": "4.1.0",
"zone.js": "0.8.12"
}
}
--- webpack.config.js
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {
stats: { modules: false },
context: __dirname,
resolve: { extensions: [ '.js', '.ts' ] },
output: {
filename: '[name].js',
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.ts$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] : '@ngtools/webpack' },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: [ 'to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize' ] },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
]
//,
//loaders: [
// {
// test: /\.css$/,
// include: [
// path.resolve(__dirname, "not_exist_path")
// ],
// loader: "style!css"
// }
// ]
},
plugins: [new CheckerPlugin()]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './ClientApp/boot.browser.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [
// Plugins that apply in production builds only
new webpack.optimize.UglifyJsPlugin(),
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.browser.module#AppModule'),
exclude: ['./**/*.server.ts']
})
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './ClientApp/boot.server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
].concat(isDevBuild ? [] : [
// Plugins that apply in production builds only
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.server.module#AppModule'),
exclude: ['./**/*.browser.ts']
})
]),
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map'
});
return [clientBundleConfig, serverBundleConfig];
};
第二种方法,
- app.component.css
@import url('~/dist/assets/css/style.css');
@import url('~/dist/assets/css/colors/green.css');
@import url('~/dist/assets/css/owl.carousel.css');
@import url('~/dist/assets/css/owl.transitions.css');
@import url('~/dist/assets/css/animate.min.css');
@import url('~/dist/assets/css/font-awesome.min.css');
错误说明:
处理请求时发生未处理的异常。 NodeInvocationException:由于错误导致预渲染失败:错误:找不到模块&#34; ./~ / app / assets / css / style.css&#34;在webpackMissingModule(L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:16287:86)位于L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:16287:191 at宾语。 (<:>> webpack_require (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist .js:20:30)在Object。 (<:>> webpack_require (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist .js:20:30)在 webpack_require (L:\ SaleStore \)的Object.hasOwn(L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:14202:77) iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:20:30)在Object。 (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:16348:81) webpack_require (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server .js:20:30)当前目录是:L:\ SaleStore \ iSaleStore \ iSaleStore Microsoft.AspNetCore.NodeServices.HostingModels.HttpNodeInstance + d__7.MoveNext()
当我删除〜/然后应用程序抛出以下错误。请看
处理请求时发生未处理的异常。 NodeInvocationException:由于错误导致预渲染失败:错误:找不到模块&#34; - !../../../../ node_modules / css-loader / index.js!./ dist / assets / css / style的CSS&#34; 在webpackMissingModule(L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:16390:75) 在对象。 (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ DIST \主server.js:16390:226) 在 webpack_require (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:20:30) 在对象。 (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ DIST \主server.js:17967:22) 在 webpack_require (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:20:30) 在L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:10401:22 在对象。 (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ DIST \主server.js:10405:2) 在 webpack_require (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:20:30) 在对象。 (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ DIST \主server.js:16131:88) 在 webpack_require (L:\ SaleStore \ iSaleStore \ iSaleStore \ ClientApp \ dist \ main-server.js:20:30) 当前目录是:L:\ SaleStore \ iSaleStore \ iSaleStore