有没有办法使用 Vue 路由器路由到 html 文件?

Is there a way to route to html files with Vue router?

您好,我正在使用 VueJS 和 webpack 模板。我有一堆组件可以很容易地用 Vue Router 显示。但是,我的组织使用 Robot Framework 进行测试,我们使用以下命令生成 HTML 页面:

python -m robot.testdoc /tests/directory /destination.html

这就是我使用路由器的基本方式:

import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/components/Main.vue'
import Component1 from '@/components/Component1.vue'
import Component2 from '@/components/Component2.vue'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      mode: history,
      name: 'Main',
      component: Main
    },
    {
      path: '/component1',
      mode: history,
      name: 'Component1',
      component: Component1
    },
    {
      path: '/component2',
      mode: history,
      name: 'Component2',
      component: Component2
    }
  ]
})

有没有办法使用 Vue Router 路由到 HTML 文件?

首先你需要 html-loader:

yarn add html-loader | npm install html-loader

然后您需要更新您的 webpack.config.js 文件并在您的 rules 中添加一个条目来处理 .html 扩展:

{
    test: /\.(html)$/,
    exclude: /(node_modules)/,
    use: {
      loader: "html-loader"
    }
}

然后您可以像导入组件一样导入 .html 文件:

import Destination from '/path/to/destination.html'

现在将 component 视为对象并利用 template 属性 来提供静态 HTML 文件:

 {
  path: '/destination',
  mode: history,
  name: 'destination',
  component: { template: Destination }
}

1.install html-装载机

npm install --save-dev html-loader

2.use 下面的代码 vue.config.js 或 Webpack.config.js

对于webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: 'html-loader',
      },
    ],
  },
};

Vue cli 用户vue.config.js

module.exports = {
    chainWebpack: config => {
        config.module
          .rule('html')
          .test(/\.html$/)
          .use('html-loader')
          .loader('html-loader')
      }
}

只需在您的

中添加路由器
{
        path: '/print',
        name: 'print',

        component: () => import('../pages/print.html'),

    },

关于 vue 的更多信息 https://cli.vuejs.org/guide/webpack.html#replacing-loaders-of-a-rule