如何修复“为组件AppComponent指定的模板不是字符串”

时间:2019-04-12 06:53:14

标签: angular

我一直按照https://github.com/me-12/single-spa-portal-example的要求进行操作,并在新的Angular 6应用程序中实现了该功能,并且可以正常工作,但是当我尝试将其与旧的现有应用程序和{{1组件上的}}无法正常工作,说“为组件templateUrl指定的模板不是字符串”。

我已通过下面的链接Loading second component causes "The template specified for component SidebarComponent is not a string",但与我的要求不符

我认为在设置规则时,AppComponent中缺少一些内容,您可以在其完整的webpack.config.js文件下面查看

webpack.config.js

i m,除了其中的templateUrl

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,
    }
};

应该能够像在新创建的项目中一样工作。

1 个答案:

答案 0 :(得分:0)

我看到您已定义

{
    test: /\.html$/,
    loader: "raw-loader"
},

{ test: /\.(html)$/, loader: "raw-loader" }

,但是angular希望使用string-loader作为高级版本。 您可以尝试像下面这样替换它吗?

npm install to-string-loader

并且:

{
    test: /\.html$/,
    loader: "to-string-loader"
},
{ test: /\.(html)$/, loader: "to-string-loader" }