1. 程式人生 > >vue.js搭建使用者管理系統練手(二)----頁面框架搭建

vue.js搭建使用者管理系統練手(二)----頁面框架搭建

專案程式碼完整地址是:https://github.com/lightTrace/vue.js
可以用來參考對比,但是由於是完整的,會比我下面一節一節來學習的複雜,但也都是入門的東西,可以看完一遍自我對照使用。

首先安裝 npm install -g vue-cli 來進行vue框架的快速搭建,安裝好後在任意目錄執行

vue init webpack myproject

就會生成一個vue專案,首先加入一個使用者元件,在components下新建Customers.vue:

<template>
  <div class="customers">
    customers
  </div
>
</template> <script> export default { name: 'customers', data () { return { } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped></style>

同樣在components目錄下新建About.vue:

<template>
  <div class
="about container" >
about </div> </template> <script> export default { name: 'about', data () { return { } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped></style>

在index.html中引入bootstrap,index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>customers</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css
">
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js
"></script>
  </body>
</html>

然後通過main.js來實現路由邏輯,main.js程式碼如下:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import Customers from './components/Customers'
import About from './components/About'

Vue.config.productionTip = false

Vue.use(VueRouter)
//設定路由
const router = new VueRouter({
    mode:"history",
    base:__dirname,
    routes:[
       {path:"/",component:Customers},
       {path:"/about",component:About}
    ]
})

/* eslint-disable no-new */
new Vue({
  router,
  template:`
    <div id="app">
    <nav class="navbar navbar-default">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">使用者管理系統</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li ><router-link to="/">主頁</router-link></li>
            <li ><router-link to="/about">關於我們</router-link></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
    <router-view></router-view>
    </div>
  `
}).$mount("#app")

這裡的main.js是載入專案的入口檔案,$mount(“#app”)是將該例項掛載到index.html中的

的標籤下面,用它的模版去完全替代index.html中的。

這裡寫圖片描述