使用vue cli進行開發,所有表現都會通過注入在index.html表現。
如果要實現多個頁面節點,一般通過路由來實現,路由有兩種模式,分別是默認的hash模式,History模式,它們的區別是:
hash模式,產生的目錄結構為:www.doamin.com/#/good
history模式,產生的目錄結構為:www.doamin.com/good/
配置的方法是定義router/router.js文件
const router = new Router({ mode: 'history', routers, });
即使是上面的方法,但對傳統網頁有所愛好者依然會不滿意,比如會網友問:
當前目前如果有多個頁面怎么辦?
比如是否能實現這樣的網頁結構:
www.z01.com/pub/index.html
www.z01.com/pub/listpage.shtml
答案是有的,偉大領袖告訴我們自力更生豐衣足食,讓我們動起手來!
1 准備工作
1.1 先查看版本
npm -V npm@6.12.0 C:\Program Files\nodejs\node_modules\npm vue -V @vue/cli 4.1.2
1.2 創建項目
vue create templates
會創建一個名為templates的項目
目錄結構為:

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/
看到這個頁面:

2 多頁面配置
2.1 修改目錄
1、在src目錄下創建一個pages文件夾,
2、在pages文件夾下面創建index文件夾,
修改之后文件夾結構為:

3、把public文件夾下的index.html移到index文件夾,
4、把components文件夾下面的HelloWorld.vue移到index文件夾,(這里可以不移動,因為這個是默認生成的一個例子,實際開發中,創建項目生成的這個頁面我們根本用不到,移動只是為了方便做例子,不另外寫一個index頁面)
5、把src文件夾下的App.vue和main.js都移到index文件夾下面
這時候目錄結構:

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>