我一直在遵循这篇文章来满足我的需求,并在新的angular 6应用程序中实现了相同的模板,它工作得很好,但当我试图将其与旧的现有应用程序集成时,我遇到了问题,组件上的templateUrl不能工作,因为“为组件AppComponent指定的模板不是字符串”。

我已经通过以下链接Loading second component causes "The template specified for component SidebarComponent is not a string",但它不符合我的要求
在设置规则时,我想我的webpack.config.js遗漏了一些东西。您可以在它的完整webpack.config.js文件下面查看。
const path = require('path');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = function (env) {
const analyzeBundle = !!(env && env.analyzeBundle);
const prodMode = !!(env && env.prod);
const plugins = [
new ContextReplacementPlugin(
/(.+)?angular(\\|\/)core(.+)?/,
path.resolve(__dirname, './src')
),
new AngularCompilerPlugin({
mainPath: path.resolve(__dirname, 'src/singleSpaEntry.ts'),
tsConfigPath: path.resolve(__dirname, 'tsconfig.json'),
sourceMap: !prodMode,
skipCodeGeneration: !prodMode,
platform: 0,
hostReplacementPaths: {
"environments/environment.ts": prodMode ? "environments/environment.prod.ts" : "environments/environment.ts"
}
})
];
if (analyzeBundle) {
plugins.push(new BundleAnalyzerPlugin());
}
const devTypescriptLoader = [
{
test: /\.ts$/,
loader: '@ngtools/webpack'
}
];
const prodTypescriptLoader = [
{
"test": /\.js$/,
"use": [
{
"loader": "@angular-devkit/build-optimizer/webpack-loader",
"options": {
"sourceMap": false
}
}
]
},
{
test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
use: [
{
"loader": "@angular-devkit/build-optimizer/webpack-loader",
"options": {
"sourceMap": false
}
},
'@ngtools/webpack'
]
}
];
const typescriptLoader = prodMode ? prodTypescriptLoader : devTypescriptLoader;
return {
entry: {
singleSpaEntry: 'src/singleSpaEntry.ts',
// store: 'src/store.ts'
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'release'),
libraryTarget: 'umd',
library: 'DoneApp'
},
module: {
rules: [
{
test: /\.html$/,
loader: "raw-loader"
},
{
test: /\.css$/,
use: ["style-loader", 'css-loader', 'postcss-loader', 'to-string-loader']
},
{
test: /\.(jpe?g|png|webp|gif|otf|ttf|woff2?|ani)$/,
loader: "url-loader",
options: {
name: "[name].[ext]",
limit: 10000,
publicPath: '/DoneApp/'
}
},
{
test: /\.(eot|svg|cur)$/,
loader: "file-loader",
options: {
name: "[name].[ext]",
publicPath: '/DoneApp/'
}
},
{
exclude: [
path.join(process.cwd(), "src/styles.scss")
],
test: /\.scss$|\.sass$/,
use: ["exports-loader?module.exports.toString()", "css-loader", "sass-loader", 'postcss-loader', 'to-string-loader']
},
{
include: [
path.join(process.cwd(), "src/styles.scss")
],
test: /\.scss$|\.sass$/,
use: ["style-loader", "css-loader", "sass-loader", 'postcss-loader', 'to-string-loader']
}
//,{ test: /\.(html)$/, loader: "raw-loader" },
].concat(typescriptLoader)
},
optimization: {
minimizer: [
new UglifyJsPlugin({
sourceMap: false,
parallel: true,
uglifyOptions: {
ecma: 6,
warnings: false,
ie8: false,
mangle: true,
compress: {
pure_getters: true,
passes: 2
},
output: {
ascii_only: true,
comments: false
}
}
})
]
},
resolve: {
extensions: [".ts", ".js"],
modules: [
__dirname,
'node_modules'
]
},
mode: 'development',
devtool: prodMode ? 'none' : 'inline-sourcemap',
externals: [],
plugins: plugins,
}
};中的templateUrl除外。
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styles: ['./app.component.css']
})应该能够工作,因为它是在我的新创建的项目工作。
发布于 2019-07-17 21:13:32
我看到你定义了
{
test: /\.html$/,
loader: "raw-loader"
},和
{ test: /\.(html)$/, loader: "raw-loader" }但angular希望在高级版本中使用to-string-loader。你能像下面这样替换它吗:
npm install to-string-loader和:
{
test: /\.html$/,
loader: "to-string-loader"
},
{ test: /\.(html)$/, loader: "to-string-loader" }https://stackoverflow.com/questions/55646041
复制相似问题