首页 > web前端 > js教程 > vue-cli创建的项目,配置多页面的实现方法

vue-cli创建的项目,配置多页面的实现方法

亚连
发布: 2018-05-30 10:30:43
原创
2216 人浏览过

下面我就为大家分享一篇vue-cli创建的项目,配置多页面的实现方法,具有很好的参考价值,希望对大家有所帮助。

vue官方提供的命令行工具vue-cli,能够快速搭建单页应用。默认一个页面入口index.html,那么,如果我们需要多页面该如何配置,实际上也不复杂

假设要新建的页面是rule,以下以rule为例

创建新的html页面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<!DOCTYPE html>

<html>

    <head>

        <meta charset="utf-8">

        <meta name="viewport" content="width=device-width,initial-scale=1.0">

        <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport">

        <meta http-equiv="pragma" content="no-cache">

        <meta http-equiv="cache-control" content="no-cache">

        <meta http-equiv="expires" content="0">

        <title></title>

    </head>

    <body>

        <span style="color:#000000;"><p id="rule"></p></span>

        <!-- built files will be auto injected -->

    </body>

</html>

登录后复制

创建入口文件Rule.vue和rule.js,仿照App.vue和main.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<template>

    <p id="rule">

        <router-view></router-view>

    </p>

</template>

<script>

    export default {

        name: &#39;lottery&#39;,

        data() {

            return {

            }

        },

        mounted: function() {

             

            this.$router.replace({

                    path:&#39;/rule&#39;

            });

        },

    }

</script>

<style lang="less">

    body{

        margin:0;

        padding:0;

    }

</style>

登录后复制

rule.js

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

import Vue from &#39;vue&#39;

import Rule from &#39;./Rule.vue&#39;

import router from &#39;./router&#39;

import $ from &#39;jquery&#39;

//import vConsole from &#39;vconsole&#39;

import fastclick from &#39;fastclick&#39;

Vue.config.productionTip = false

fastclick.attach(document.body)

Vue.config.productionTip = false;

  

/* eslint-disable no-new */

new Vue({

 el: &#39;#rule&#39;,

 router,

 template: &#39;<Rule/>&#39;,

 components: { Rule },

})

登录后复制

修改config/index.js

build添加rule地址,即编译后生成的rule.html的地址和名字

1

2

3

4

5

6

build: {

  // Template for index.html

  index: path.resolve(__dirname, &#39;../dist/index.php&#39;),

  rule: path.resolve(__dirname, &#39;../dist/rule.php&#39;),

  ……

 }

登录后复制

rule: path.resolve(__dirname, '../dist/rule.php')表示编译后再dist文件下,rule.html编译后文件名为rule.php

修改build/webpack.base.conf.js

配置entry

1

2

3

4

entry: {

  app: &#39;./src/main.js&#39;, 

  rule: &#39;./src/rule.js&#39;

 },

登录后复制

修改build/webpack.dev.conf.js

在plugins增加

1

2

3

4

5

6

new HtmlWebpackPlugin({

   filename: &#39;rule.html&#39;,

   template: &#39;rule.html&#39;,

   inject: true,

   chunks:[&#39;rule&#39;]

  }),

登录后复制

修改build/webpack.prod.conf.js

在plugins增加

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

new HtmlWebpackPlugin({

   filename: config.build.rule,

   template: &#39;rule.html&#39;,

   inject: true,

   minify: {

    removeComments: true,

    collapseWhitespace: true,

    removeAttributeQuotes: true

    // more options:

    // https://github.com/kangax/html-minifier#options-quick-reference

   },

   // necessary to consistently work with multiple chunks via CommonsChunkPlugin

   chunksSortMode: &#39;dependency&#39;,

   chunks: [&#39;manifest&#39;,&#39;vendor&#39;,&#39;rule&#39;]

  }),

登录后复制

以上全部

当后台地址跳转到你的新建的页面后,由于现在配置的默认路由是公用的,可自己配置多个路由文件,分别引用。

可在Rule.vue中路由跳转到指定路由,以实现页面控制

1

2

3

4

5

6

mounted: function() {

             

    this.$router.replace({

        path:&#39;/rule&#39;

    });

},

登录后复制

上面是我整理给大家的,希望今后会对大家有帮助。

相关文章:

基于vue1和vue2获取dom元素的方法

nodejs+mongodb aggregate级联查询操作示例

JS实现将链接生成二维码并转为图片的方法

以上是vue-cli创建的项目,配置多页面的实现方法的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板