我們采用實戰教學模式並結合ElementUI組件庫,將所需知識點應用到實際中,以最快速度帶領大家掌握Vue的使用
創建工程
注意:命令行都需要使用管理員模式運行
- 創建一個名為hello-vue的工程
vue init webpack hello-vue
- 安裝依賴,需要安裝vue-router、element-ui、sass-loader和node-sass四個插件
# 進入工程目錄
cd hello-vue
# 安裝vue-router
npm install vue-router --save-dev
# 安裝element-ui
npm i element-ui -S
# 安裝依賴
npm install
# 安裝sass加載器(編譯css文件)
cnpm install sass-loader node-sass --save-dev
# 啟動測試
npm run dev
- npm命令解釋
- npm install moduleName: 安裝模塊到項目目錄下
- npm install -g moduleName: -g表示將模塊安裝到全局,具體安裝到磁盤哪個位置,要看npm config prefix的位置
- npm install -save moduleName: --save表示將模塊安裝到項目目錄下,並在package文件的dependencies節點寫入依賴,-S為該命令的縮寫
- npm install -save-dev moduleName:--save-dev表示講模塊安裝到項目目錄下,並在package文件的devDependencies節點寫入依賴,-D為該命令的縮寫
創建登錄頁面
- static:存放靜態資源
- 創建router和views文件夾
- router:存放路由主配置文件index.js
- views:存放Vue視圖組件
- components:存放Vue功能組件
- assets:存放資源文件
- 創建首頁視圖,在views目錄下創建一個名為Main.vue的視圖組件
<template>
<div>首頁</div>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped>
</style>
- 創建登錄頁視圖,在views目錄下創建一個名為Login.vue的視圖組件,其中el-*的元素為ElementUI組件
<template>
<div>
<el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
<h3 class="login-title">歡迎登錄</h3>
<el-form-item label="賬號" prop="username">
<el-input type="text" placeholder="請輸入賬號" v-model="form.username"/>
</el-form-item>
<el-form-item label="密碼" prop="password">
<el-input type="password" placeholder="請輸入密碼" v-model="form.password"/>
</el-form-item>
<el-form-item>
<el-button type="primary" v-on:click="onSubmit('loginForm')">登錄</el-button>
</el-form-item>
</el-form>
<el-dialog
title="溫馨提示"
:visible.sync="dialogVisible"
width="30%"
:before-close="handleClose">
<span>請輸入賬號和密碼</span>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="dialogVisible = false">確 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
name:"Login",
data(){
return {
form:{
username: '',
password: ''
},
//表單驗證,需要再el-form-item 元素中增加prop屬性
rules:{
username: [
{required:true,message:'賬號不能為空',trigger:'blur'}
],
password: [
{required: true,message: '密碼不能為空',trigger:'blur'}
]
},
//對話框顯示和隱藏
dialogVisible:false
}
},
methods:{
onSubmit(formName) {
//為表單綁定驗證功能
this.$refs[formName].validate((valid) =>{
if (valid){
//使用 vue-router路由到指定頁面,該方式稱之為編程式導航
this.$router.push("/main");
} else {
this.dialogVisible = true;
return false;
}
});
}
}
}
</script>
<style lang="scss" scoped>
.login-box{
border: 1px solid #DCDFE6;
width: 350px;
margin:180px auto;
padding:35px 35px 15px 35px;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
box-shadow:0 0 25px #909399;
}
.login-title{
text-align:center;
margin:0 auto 40px auto;
color:#303133;
}
</style>
- 通過路由交互獨立的組件
5.1 設置路由主配置文件index.js
5.2 主函數main.js設置
5.2.1 導入路由主配置(中轉站)
5.2.2 導入ElementUI配置
5.3 App.vue組件中顯示路由跳轉界面
注意:
啟動時可能出現編譯失敗(The “path” argument must be of type string. Received undefined),原因是sass版本過高,去package.json文件中將sass版本降至7.3.1,然后重新npm install即可
5.4 通過路由手動跳轉頁面(#表示路由)
- 目前主頁為空
- 手動跳轉到main頁面
- 手動跳轉到login頁面
路由嵌套
嵌套路由又稱子路由,在實際應用中,通常由多層嵌套的組件組合而成。同樣的,URL中各段動態路徑也按某種結構對應嵌套的各層組件,例如:
- 用戶信息組件,在views/user目錄下創建一個名為Profile.vue的視圖組件
- 用戶列表組件,在views/user目錄下創建一個名為List.vue的視圖組件
- 配置嵌套路由修改router目錄下index.js路由配置文件
import Vue from 'vue'
import Router from 'vue-router'
import Main from '../views/Main'
import Login from '../views/Login'
// 用於嵌套的路由組件
import UserProfile from '../views/user/Profile'
import UserList from '../views/user/List'
Vue.use(Router)
export default new Router({
routes: [
{
// 登錄頁
path: '/login',
name: 'Login',
component: Login
},
{
// 首頁
path: '/main',
name: 'Main',
component: Main,
// 配置嵌套路由
children: [
{
path: '/user/profile',
component: UserProfile
},
{
path: '/user/list',
component: UserList
}
]
}
]
});
說明:主要在路由配置中添加children數組配置,用於在該組件下設置嵌套路由
- 修改首頁視圖,我們修改Main.vue視圖組件,此處使用了ElementUI布局容器組件,代碼如下:
<template>
<div>
<el-container>
<el-aside width="200px">
<el-menu :default-openeds="['1']">
<el-submenu index="1">
<template slot="title"><i class="el-icon-caret-right"></i>用戶管理</template>
<el-menu-item-group>
<el-menu-item index="1-1">
<router-link to="/user/profile">個人信息</router-link>
</el-menu-item>
<el-menu-item index="1-2">
<router-link to="/user/list">用戶列表</router-link>
</el-menu-item>
</el-menu-item-group>
</el-submenu>
<el-submenu index="2">
<template slot="title"><i class="el-icon-caret-right"></i>內容管理</template>
<e1-menu-item-group>
<el-menu-item index="2-1">分類管理</el-menu-item>
<el-menu-item index="2-2">內容列表</el-menu-item>
</e1-menu-item-group>
</el-submenu>
</el-menu>
</el-aside>
<el-container>
<el-header style="text-align: right; font-size: 12px">
<el-dropdown>
<i class="el-icon-setting" style="margin-right:15px"></i>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>個人信息</el-dropdown-item>
<el-dropdown-item>退出登錄</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</el-header>
<el-main>
<router-view/>
</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped lang="scss">
.el-header {
background-color: #048bd1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
參數傳遞及重定向
- 前端傳入參數
- 路由配置支持傳遞參數
- 組件接收參數
- 重定向
4.1 路由配置
4.2 組件路由鏈接
路由模式與404
- 路由模式有兩種
- hash:路徑帶 # 符號,如 http://localhost/#/login
- history:路徑不帶 # 符號,如 http://localhost/login
修改路由配置,代碼如下
export default new Router({
mode: 'history',
routes: [
]
});
- 404
2.1 新建NotFound組件
2.2 路由配置
import NotFound from "../views/NotFound"
// routes中添加404路由配置
{
path: '*', // 找不到的路徑就走這里
component: NotFound
}
路由鈎子與異步請求
beforeRouteEnter:在進入路由前執行
beforeRouteLeave:在離開路由前執行
參數說明:
- to:路由將要跳轉的路徑信息
- from:路由跳轉前的路徑信息
- next:路由的控制參數
- next():跳入到下一個頁面
- next('/path'):改變路由的跳轉方向,使其跳到另一個路由
- next(false):返回原來的頁面
- next(vm => {}):僅在beforeRouteEnter中使用,vm是組件實例
在鈎子中使用異步請求
- 安裝axios
npm install axios -s
- main.js中導入axios
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(axios, VueAxios)
- 准備數據:只有我們的static目錄下的文件時可以被訪問到的,所以我們把靜態文件都放入到該目錄下
// 靜態數據存放的位置
static/mock/data.json // 測試數據一般放在static/mock文件夾下
- 在beforeRouteEnter中進行異步請求
<template>
<div>
<h1>個人信息</h1>
{{id}}
</div>
</template>
<script>
export default {
props: ['id'],
name: "UserProfile",
beforeRouteEnter: (to, from, next) => {
console.log("進入路由之前"); // 加載數據
next(vm => {
vm.getData(); // 進入路由之前執行getData
})
},
beforeRouteLeave: (to, from, next) => {
console.log("進入路由之后");
next();
},
methods: {
getData: function () {
this.axios({
methods: 'get',
url: 'http://localhost:8080/static/mock/data01.json'
}).then(function (response) {
console.log(response)
})
}
}
}
</script>
<style scoped>
</style>