我想为用vue cli 3创建的网页设置标题,因此查看了public/index.html个.在那里,我找到了<title><%= htmlWebpackPlugin.options.title %></title>.

如何在vue cli 3项目中设置和修改htmlWebpackPlugin.options.title

推荐答案

考虑到这个问题的受欢迎程度,我决定添加一个详细的答案,并附上参考资料,以使其更加真实和完整.我还就这个主题创建了an article个,并在thisthis门课程中涵盖了这个主题.

虽然问题是设置htmlWebpackPlugin.options.title,但最终的效果是更改网页的标题.

1. Most convenient and trivial solution

最简单的方法是修改public/index.html并硬编码标题.

<!DOCTYPE html>
<html lang="">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title>
        <%= htmlWebpackPlugin.options.title %>
    </title>
</head>

<body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it 到 continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be au到 injected -->
</body>

</html>

这是vue cli生成的默认值public/index.html.在这方面,你只需要改变

    <title>
        <%= htmlWebpackPlugin.options.title %>
    </title>

<title>Title of your choice</title>

2. Change the name field in package.json

Another simple solution is 到 change the "name": "your-project-name". However, there are many restrictions on the name you can use in package.json. You can read more about this here. Basically, package.json must contain a name and that must be lowercase and one word, and may contain hyphens and underscores.

3. Using pages field in vue.config.js

vue.config.js is an optional file that you can add 到 provide additional configurations 到 Vue CLI and this file, if present, will be au到matically loaded by Vue CLI. You need 到 create vue.config.js in the root folder - the folder containing you package.json file.

According 到 Vue documentation, you can use pages field 到 define entrypoint for multi-page app. However, you can also use this 到 define title for single page app as well. Create vue.config.js in the root direc到ry and add pages field 到 your exports as follows:

module.exports = {
  pages: {
    index: {
      // entry for the page
      entry: 'src/main.js',
      title: 'My Title',
    },
  }
}

Note that if you are already running development server, this change will be reflected only when you s到p and restart the development server. In other words, these changes will not be hot reloaded.

4. Chaining Webpack

你可以在vue.config.js中 Select chain Webpack,如下所示

module.exports = {
    chainWebpack: config => {
        config
            .plugin('html')
            .tap(args => {
                args[0].title = "My Vue App";
                return args;
            })
    }
}

Note that similar 到 solution 3, this change will be reflected only when you s到p and restart the development server, in case you are already running development server. In other words, these changes will not be hot reloaded.

5. Modify title in lifecycle hooks using JavaScript

The next solution in the list is 到 use JavaScript 到 modify the title. You can do this either in mounted lifecycle hook of your root component or if you want different title for different routes, you can do the same for components loaded by each route.

<script>
export default {
  data() {
    return {
      //
    };
  },
  mounted() {
    document.title = 'new title'
  }
}
</script>

6. Use Vue Meta

Finally you can use Vue Meta 到 manage all metadata for your Vue app including title. First you need 到 add Vue Meta 到 your project and then use metaInfo field as shown below 到 configure metadata for your page or route.

{
  metaInfo: {
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { title: 'My title'}
    ]
  }
}

Conclusion

前4个解决方案是更改标题的静态方式,或者换句话说,您不能使用这些方式在运行时更改标题.此外,所有这些都不是热重新加载.最后两个选项使用JavaScript,可以在运行时操纵标题.

Node.js相关问答推荐

FireStore事务似乎已允许并发编辑?

在编译时强制不缩小类型范围

为什么我的过滤器无法在我在下面编写的 Google Analytics 4 应用程序脚本代码中工作?我该如何修复它?

Nest Js - 在 multer 拦截器中如何设置最大文件上传大小并进行可选文件上传

在 Docker 容器内创建一个 cron 作业(job)来执行 run.js 文件中的函数

如果我在父文件夹中运行,子进程生成不起作用

发布请求不使用 Nodejs 更新 MongoDB 中的标头信息

在对象数组中的数组中嵌套 $lookup - Mongodb

MongoDB - 查找查询以判断是否存在少量字段,结合字段上的 AND

在路由上使用中间件时,Nodejs Express 应用程序失败

多字段传递获取查询失败

NodeJS 中的流 API 数据如何流动?

后端位于 Docker 容器中时的 SvelteKit SSR fetch()

如何在 TypeScript 中输出 Hackerrank 二叉树问题?

如何使用 superagent/supertest 链接 http 调用?

在 Node.js 与 Cron 作业(job)中设置间隔?

如何可靠地散列 JavaScript 对象?

从目录 node Js 中检索文件

Node.js 与 .net 中的异步/等待

用于轻松部署和更新的 Node.js 设置