xiaoliang-

使用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的项目
目录结构为:


 
初始结构.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>

分类:

技术点:

相关文章: