【轉載】VUE入門教程
vue-cli是官方提供的一個腳手架,用於快速生成一vue專案,有點類似java中使用maven構建專案
需要環境
Node.js : http://nodejs.cn/download/ 安裝完後在Windows的cmd視窗輸入 node -v及npm -v 如果有版本號,那麼說明安裝成功 也可以安裝淘寶的映象,這樣下載的話會快很多,安裝淘寶映象後可以使用cnpm指令
# -g 全域性安裝
npm install cnpm -g
npm config set registry https://registry.npm.taobao.org
npm install cnpm -g
安裝位置:C:\Users\Administrator\AppData\Roaming\npm
安裝vue-cli
#在命令臺輸入
cnpm install vue-cli -g
#檢視是否安裝成功
vue list
建立第一個vue-cli程式
1、在本地磁碟建立一個空資料夾用來存放專案 D:\vue\vuenote 2、使用控制檯在該目錄下執行建立vue應用程式指令
D:\vue\vuenote>vue init webpack first-vue
3、一路選擇no 4、進入專案目錄,安裝依賴
D:\vue\vuenote>cd first-vueD:\vue\vuenote\first-vue>cnpm install
5、啟動專案
npm run dev
開啟瀏覽器輸入 http://localhost:8080/
webpack
webpack是一個現代JavaScript應用程式的靜態模組打包器(module bundler)。當webpack處理應用程式時,它會遞迴地構建一個依賴關係圖(dependency graph),其中包含應用程式需要的每個模組,然後將所有這些模組打包成一個或多個bundle.
webpack的使用
1、在本地磁碟上建立一個空目錄,並使用idea開啟
2、按如下結構建立目錄和檔案
3、在hello.js暴露一個sayhai的方法
exports.sayHai=function () { document.write("<h1>hello world</h1>" )}
4、在main.js匯入該方法
var hello=require('./hello')hello.sayHai()
5、在webpack.config.js中配置打包
module.exports={ entry:'./modules/main.js', output:{ filename:'./js/bundle.js' }}
6、在idea控制檯執行 webpack指令 執行webpack指令後,會在當前專案的生成dist/js/bundle.js 7、在index.html中引入bundle.js檔案
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <script src="dist/js/bundle.js"></script></body></html>
Vue-Router路由
Vue Router是Vue.js官方的路由管理器(路徑跳轉)。它和Vue.js的核心深度整合,讓構建單頁面應用變得易如反掌。
安裝路由
使用idea在當前專案的控制檯上輸入指令
cnpm install vue-router --save-dev
路由的使用
1、在component目錄下建立一個vue元件Content.vue
import Vue from 'vue'
import VueRouter from 'vue-router'
import Content from "../components/Content";
//安裝路由
Vue.use(VueRouter);
export default new VueRouter({
routes:
[
{
//路由路徑
path: '/content',
name: 'content',
//跳轉的元件
component: Content
}
]
})
2、在當前專案下建立router目錄,router目錄下建立用來配置路由的配置檔案index.js index.js內容如下:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Content from "../components/Content";
//安裝路由
Vue.use(VueRouter);
export default new VueRouter({
routes:
[
{
//路由路徑
path: '/content',
name: 'content',
//跳轉的元件
component: Content
}
]
})
3、在App.vue中配置請求路由
<template>
<div id="app">
<img src="./assets/logo.png">
//請求路由
<router-link to="/content">內容頁</router-link>
//路由結果在此處展示
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
components: {
}
}
</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>
vue+elementUI
vue配合elementUI可以使我們的頁面更加美觀
elementUI的使用
1、建立一個新的vue專案
vue init webpack vue_element
2、安裝外掛(vue-router、element-ui、sass-loader、node-sass)
# 進入工程目錄
cd vue_element
# 安裝 vue-router
npm install vue-router --save-dev
# 安裝 element-ui
npm i element-ui -S
# 安裝依賴
npm install
# 安裝 SASS 載入器
cnpm install sass-loader node-sass --save-dev
# 啟動測試
npm run dev
3、建立一個Login.vue元件,內容如下:
<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>
4、配置路由
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
//安裝路由
Vue.use(VueRouter)
export default new VueRouter({
routes:[
{
path:'/login',
name:'login',
component:Login
}
]
})
5、在main.js引入路由和elementUI
// 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 router from './router'
//匯入elementUI
import ElementUI from "element-ui"
//匯入element css
import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false
Vue.use(router);
Vue.use(ElementUI)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: h => h(App),//ElementUI規定這樣使用
})
6、在App.vue中請求路由
<template>
<div id="app">
<router-link to="/login">登入</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
components: {
}
}
</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>
7、測試 npm run dev
注意:如果專案執行失敗,可以在package.json裡降低sass-loader和node-sass的版本
"sass-loader": "^7.3.1",
"node-sass": "^4.9.0",
巢狀路由
簡單說就是在路由裡再套一個子路由
1、建立一個作為子路由Profile.vue元件
<template>
<h1>使用者列表</h1>
</template>
<script>
export default {
name: "List"
}
</script>
<style scoped>
</style>
2、Main.vue裡請求路由
<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>
<el-menu-item-group>
<el-menu-item index="2-1">分類管理</el-menu-item>
<el-menu-item index="2-2">內容列表</el-menu-item>
</el-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: #B3C0D1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
3、測試
引數傳遞
引數傳遞過程:url請求路徑—->路由接收引數—->跳轉套元件顯示引數
1、url請求路徑
<router-link :to="{name:'Profile',params:{id:1} }">個人資訊</router-link>
2、路由接收引數
方式一:
{path:'/user/profile/:id',name:'Profile',component:Profile},
方式二:
{path:'/user/profile/:id',name:'Profile',component:Profile,props:true}
3、元件模板展示引數
方式一:
{ {$route.params.id} }
方式二:
<template>
<div>
{ {id} }
</div>
</template>
<script>
export default {
//接收路由傳過來的id
props:['id'],
name: "Profile"
}
</script>
<style scoped>
</style>
路由鉤子與非同步請求
路由模式
hash:路徑帶 # 符號(預設),如 http://localhost/#/login history:路徑不帶 # 符號,如 http://localhost/login
路由鉤子與非同步請求
beforeRouteEnter:在進入路由前執行 beforeRouteLeave:在離開路由前執行 類似於過濾器,在進入模板前可以使用路由鉤子進行非同步請求資料,並在模板展示
<template>
<div>
{ {info.url} }
</div>
</template>
<script>
export default {
props:['id'],
name: "Profile",
beforeRouteEnter:(to,from,next)=>{
console.log("進入頁面之前");
next(vm=>{
//進入路由之前執行getData方法
vm.getData()
});
},
beforeRouteLeave:(to,from,next)=>{
console.log("離開頁面之前")
next();
},
//返回請求的資料
data(){
return{
info:{
}
}
},
methods:{
getData:function () {
//使用axios非同步請求資料
this.axios({
method:'get',
url:'http://localhost:8080/static/mock/data.json'
}).then(res=>(this.info=res.data))
}
}
}
</script>
<style scoped>
</style>