Vue-1 Vue cli3 多页面配置

1 准备工作

1.1 先查看版本
npm -V
npm@6.9.0 D:\Program Files\nodejs\node_modules\npm
vue -V
vue == 3.11.0
1.2 创建项目
vue create templates

会创建一个名为templates的项目
目录结构为:


初始结构.png
npm run serve

App running at:
- Local:   http://localhost:8080/
- Network: http://192.168.43.36:8080/

Note that the development build is not optimized.
To create a production build, run npm run build.

运行npm run serve,可以在

http://localhost:8080/
http://192.168.43.36:8080/

看到这个页面:


初始index.png

2 多页面配置

2.1 修改目录

1、在src目录下创建一个pages文件夹,
2、在pages文件夹下面创建index文件夹,
修改之后文件夹结构为:


第一次修改目录结构.jpg

3、把public文件夹下的index.html移到index文件夹,
4、把components文件夹下面的HelloWorld.vue移到index文件夹,(这里可以不移动,因为这个是默认生成的一个例子,实际开发中,创建项目生成的这个页面我们根本用不到,移动只是为了方便做例子,不另外写一个index页面)
5、把src文件夹下的App.vue和main.js都移到index文件夹下面

这时候目录结构:


第一次移动文件.png
2.2 理一下逻辑
index.html

这个文件便是打开首页显示的文件。

<!DOCTYPE html>
<html lang="en">
  <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>templates</title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but templates doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div> <!-- 记住这里!!!!-->
    <!-- built files will be auto injected -->
  </body>
</html>

记住 id="app"这行

App.vue

内容如下:

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'app',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

这里就是写index.html中id="app"这个div的内容。
并且引入了HelloWorld.vue

components: {
    HelloWorld
  }

包含了这个组件

<HelloWorld msg="Welcome to Your Vue.js App"/>

给HelloWorld.vue传入了一个msg变量

HelloWorld.vue
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>  <!--传入的msg变量在这里 -->
    <p>
      For a guide and recipes on how to configure / customize this project,<br>
      check out the
      <a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
    </p>
    <h3>Installed CLI Plugins</h3>
    <ul>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
      <li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
    </ul>
    <h3>Essential Links</h3>
    <ul>
      <li><a href="https://vuejs.org" target="_blank" rel="noopener">Core Docs</a></li>
      <li><a href="https://forum.vuejs.org" target="_blank" rel="noopener">Forum</a></li>
      <li><a href="https://chat.vuejs.org" target="_blank" rel="noopener">Community Chat</a></li>
      <li><a href="https://twitter.com/vuejs" target="_blank" rel="noopener">Twitter</a></li>
      <li><a href="https://news.vuejs.org" target="_blank" rel="noopener">News</a></li>
    </ul>
    <h3>Ecosystem</h3>
    <ul>
      <li><a href="https://router.vuejs.org" target="_blank" rel="noopener">vue-router</a></li>
      <li><a href="https://vuex.vuejs.org" target="_blank" rel="noopener">vuex</a></li>
      <li><a href="https://github.com/vuejs/vue-devtools#vue-devtools" target="_blank" rel="noopener">vue-devtools</a></li>
      <li><a href="https://vue-loader.vuejs.org" target="_blank" rel="noopener">vue-loader</a></li>
      <li><a href="https://github.com/vuejs/awesome-vue" target="_blank" rel="noopener">awesome-vue</a></li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
main.js
import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

render: h => h(App)这句话的意思是创建 App element
就是下面这个函数的意思

render: function (createElement) {
    return createElement(App);
}
2.3 修改文件名称

App.vue改为index.vue;
main.js改为index.js

2.4 修改文件内容
index.vue
<template>
  <div id="app">
    <img alt="Vue logo" src="../../assets/logo.png"> 修改这里
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
import HelloWorld from './HelloWorld.vue'  修改这里

export default {
  name: 'app',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

index.js
import Vue from 'vue'
import App from './index.vue'  修改这里

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

3 配置vue.config.js

在src文件夹同一级目录添加一个名为vue.config.js的js文件。


添加vue.config.js.png

内容如下:

module.exports = {
    publicPath: '/',
    outputDir: 'dist',
    assetsDir: 'assets',
    indexPath: 'index.html',
    filenameHashing:true,
    pages: {
        index: {
            entry: "./src/pages/index/index.js", // 配置入口js文件
            template: "./src/pages/index/index.html", // 主页面
            filename: "index.html", // 打包后的html文件名称
            title: "首页", // 打包后的.html中<title>标签的文本内容
            // 在这个页面中包含的块,默认情况下会包含
            // 提取出来的通用 chunk 和 vendor chunk。
            chunks: ['chunk-vendors', 'chunk-common', 'index']
        }
        },
}

具体配置请查看官方文档

启动 npm run serve
这时候可以重新打开首页。
我们做到这里就完成了首页页面配置。

4 新增一个页面

新增一个页面,随便取个名字 baicai
在pages文件夹下新增一个baicai文件夹
在白菜文件夹下新建几个文件

baicai.html
baicai.vue
baicai.js
新增baicai.jpg

编辑文件内容

baicai.html
<!DOCTYPE html>
<html lang="en">
  <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>baicai</title>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but templates doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>
baicai.vue
<template>
  <div id="app">
    <img alt="Vue logo" src="../../assets/logo.png">
    <h1>一颗数据小白菜</h1>
  </div>
</template>

<script>
export default {
  name: 'app',
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
baicai.js
import Vue from 'vue'
import App from './baicai.vue'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')

5 配置router

在src下创建router文件夹,在router文件夹下创建router.js
修改内容为:

import Vue from 'vue'
import Router from 'vue-router'

import index from '../pages/index/index.vue';
import baicai from '../pages/baicai/baicai.vue';

Vue.use(Router);

const routers = [
    {
        path: '/',
        redirect: '/index',
        component: index,
        meta: {requiresAuth: false}
    },
    {
        path: '/index',
        name: 'index',
        component: index,
        meta: { requiresAuth: false },
    },
    {
        path: '/baicai',
        name: 'baicai',
        component: baicai,
        meta: { requiresAuth: false },
    },
];
const router = new Router({
    mode: 'history',
    routers,
});

export default router;

6 重启服务

打开http://localhost:8080/baicai

baicai.png

7 首页跳转

修改index文件夹下的 index.vue

<template>
  <div id="app">
    <img alt="Vue logo" src="../../assets/logo.png">
      <div>
        <a href="baicai.html">跳转到白菜页面</a> 修改这里
      </div>
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
import HelloWorld from './HelloWorld.vue'

export default {
  name: 'app',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

重启服务,打开http://localhost:8080

跳转到白菜页面.png

点击跳转到白菜页面
就会跳转到http://localhost:8080/baicai.html

一颗数据小白菜.png

8 总结

多页面配置其实就是2个步骤:
1、配置vue.config.js中的pages
2、配置router

demo地址
GitHub

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 本文转载于前端工匠。如有侵权联系本人立刻删除 一、首先带着问题 要学习vue-ro...
    qiaoguoxing阅读 443评论 0 1
  • 本文基于工作项目开发,做的整理笔记前段时间公司有的小伙伴刚开始学习vue,就直接着手用在新项目上,以项目实战步步为...
    SeasonDe阅读 7,090评论 16 39
  • 一、前言 要学习vue-router就要先知道这里的路由是什么?为什么我们不能像原来一样直接用 标签编写链接哪?...
    浪里行舟阅读 67,523评论 12 204
  • 风路过雨的黄昏 落叶飘过窗的玻璃 我,依窗而立 点一支烟,细数雨滴 灯映在夜的城中村 寺庙的钟声阵阵响起 我,撑伞...
    木子魚說阅读 369评论 0 4
  • 对于情绪,佛法中有一个特别生动的一句话:不要被第二支毒箭所伤害。第一支毒箭是发生的事件,第二支毒箭是我们由此而产生...
    F公子阅读 1,964评论 0 2