淺學Vue 引入 <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script> Hello Vue <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <s ...
淺學Vue
引入
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
Hello Vue
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<title></title>
</head>
<body>
<div id="app">
{{message}}
</div>
<script>
var app=new Vue({
//element,綁定id
el:"#app",
data:{
message:"hello Vue!"
}
})
</script>
</body>
</html>
條件渲染
v-if,v-else
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<title></title>
</head>
<body>
<div id="app">
<h1 v-if="awesmoe">Vue is awesmoe</h1>
<h1 v-else>Oh no</h1>
</div>
<script>
var app=new Vue({
//element,綁定id
el:"#app",
data:{
awesmoe:true
}
})
</script>
</body>
</html>
v-else-if
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<title></title>
</head>
<body>
<div id="app">
<div v-if="type==='A'">A</div>
<div v-else-if="type==='B'">B</div>
<div v-else-if="type==='C'">C</div>
<div v-else>not A/B/C</div>
</div>
<script>
var app=new Vue({
//element,綁定id
el:"#app",
data:{
type:'A'
}
})
</script>
</body>
</html>
列表渲染
v-for
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<title></title>
</head>
<body>
<ul id="example-1">
<li v-for="item in items">
{{item.message}}
</li>
</ul>
<script>
var app=new Vue({
//element,綁定id
el:"#example-1",
data:{
items:[
{message:'Foo'},
{message:'Bar'}
]
}
})
</script>
</body>
</html>
事件處理
v-on
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<title></title>
</head>
<body>
<div id="example-1">
<button v-on:click="greet">Greet</button>
</div>
<script>
var app=new Vue({
//element,綁定id
el:"#example-1",
data:{
message:"Hello"
},
methods:{
greet:function(){
alert(this.message)
}
}
})
</script>
</body>
</html>
Axios
引入
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
data,json
{
"name": "百度",
"url": "https://baidu.com/",
"page": 66,
"isNonProfit": true,
"address": {
"street": "廣州",
"city": "廣東",
"country": "中國"
},
"links": [{
"name": "博客園",
"url": "https://www.cnblogs.com/"
}, {
"name": "必應",
"url": "https://www.bing.com/"
}, {
"name": "搜狗",
"url": "https://www.sougou.com/"
}]
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title></title>
</head>
<body>
<div id="app">
<div>
名稱:{{info.name}}
</div>
<div>
url:{{info.url}}
</div>
</div>
<script>
var app=new Vue({
//element,綁定id
el:"#app",
data(){
return{
info:{
name:'',
url:''
}
}
},
mounted(){
axios
.get('data.json')
.then(response=>this.info=response.data)
}
})
</script>
</body>
</html>
表單輸入綁定
v-model
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title></title>
</head>
<body>
<div id="app">
<input type="text" v-model="message" value="這個value不顯示了,雙向綁定顯示Hello"/>
<p>Message is :{{message}}</p>
</div>
<script>
var app=new Vue({
//element,綁定id
el:"#app",
data:{
message:"Hello"
}
})
</script>
</body>
</html>
組件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title></title>
</head>
<body>
<div id="app">
<ul>
<my-component-li v-for="item in items" v-bind:item="item">
</my-component-li>
</ul>
</div>
<script>
//定義一個組件
Vue.component("my-component-li",{
props:["item"],
template:'<li>{{item}}</li>'
})
var app=new Vue({
//element,綁定id
el:"#app",
data:{
items:["張三","李四","王五"]
}
})
</script>
</body>
</html>
計算屬性
將計算結果緩存,變成靜態屬性
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title></title>
</head>
<body>
<div id="app">
<p>當前時間方法:{{getCurrentTime()}}</p>
<p>當前時間屬性:{{getCurrentTime1}}</p>
</div>
<script>
var app=new Vue({
//element,綁定id
el:"#app",
//方法
methods:{
getCurrentTime:function(){
return Date.now();
}
},
//計算屬性
computed:{
getCurrentTime1:function(){
return Date.now();
}
}
})
</script>
</body>
</html>
Vue-cli
用於快速生成項目模板
下載node.js,下載地址
安裝,一路next
驗證,cmd,
node -v
,npm -v
鏡像,
npm config set registry https://registry.npm.taobao.org
下載,
npm install vue-cli -g
驗證,
vue -V
如果安裝失敗,可以先卸載,再次安裝最新版
還不行,執行
npm config list
,查看prefix,找到vue.cmd安裝目錄,加入path環境變數中進入對應路徑下,執行
vue init webpack firstvue
創建一個項目,跟著提示選擇進入創建的firstvue,執行
npm install
執行
npm run dev
瀏覽器輸入列印的網址回車
Vue-cli目錄結構
-
build
和config
:WebPack 配置文件 -
node_modules
:用於存放 npm install 安裝的依賴文件 -
src
: 項目源碼目錄 -
static
:靜態資源文件 -
.babelrc
:Babel 配置文件,主要作用是將 ES6 轉換為 ES5 -
.editorconfig
:編輯器配置 -
eslintignore
:需要忽略的語法檢查配置文件 -
.gitignore
:git 忽略的配置文件 -
.postcssrc.js
:css 相關配置文件,其中內部的 module.exports 是 NodeJS 模塊化語法 -
index.html
:首頁,僅作為模板頁,實際開發時不使用 -
package.json:項目的配置文件
name
:項目名稱version
:項目版本description
:項目描述author
:項目作者scripts
:封裝常用命令dependencies
:生產環境依賴devDependencies
:開發環境依賴
Webpack
一款模塊載入器兼打包工具,它能把各種資源,如 JS、JSX、ES6、SASS、LESS、圖片等都作為模塊來處理和使用。
安裝
npm install webpack -g
npm install webpack-cli -g
檢驗
webpack -v
webpack-cli -v
配置
-webpack
--modules
---main.js
---hello.js
-webpack.config.js
entry
:入口文件, 指定Web Pack用哪個文件作為項目的入口output
:輸出, 指定WebPack把處理完成的文件放置到指定路徑module
:模塊, 用於處理各種類型的文件plugins
:插件, 如:熱更新、代碼重用等resolve
:設置路徑指向watch
:監聽, 用於設置文件改動後直接打包
main.js
var hello=require("./hello");
hello.sayHi();
hello.js
exports.sayHi=function(){
document.write("<div>Hello Webpack</div>");
}
webpack.config.js
module.exports = {
entry: "./modules/main.js",
output: {
path: "",
filename: "./js/bundle.js" ,
},
module: {
loaders: [
{test: /\.js$/,loader: ""}
]
},
plugins: {},
resolve: {},
watch: true
}
打包
cmd進入webpack目錄下執行
webpack
vue-router
官方的路由管理器
安裝
npm install vue-router --save-dev
不成功嘗試npm install --legacy-peer-deps [email protected]
例子
.html調用.js調用.vue
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>myvue</title>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
main.js
import Vue from 'vue'
import App from './App'
import router from './router' //自動掃描裡面的路由配置
Vue.config.productionTip = false
new Vue({
el: '#app',
//配置路由
router,
components: { App },
template: '<App/>'
})
App.vue
<template>
<div id="app">
<img src="./assets/logo.png">
<h1>迪師傅</h1>
<router-link to="/main">首頁</router-link>
<router-link to="/content">內容頁</router-link>
<router-link to="/kuang">Kuang</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>
Content.vue
<template>
<h1>內容</h1>
</template>
<script>
export default {
name: "Content"
}
</script>
<style scoped>
</style>
router/index.js
import Vue from "vue";
import VueRouter from "vue-router";
import Content from "../components/Content";
import Main from "../components/Main";
import Kuang from "../components/Kuang";
//安裝路由
Vue.use(VueRouter);
//配置導出路由
export default new VueRouter({
routes: [
{
//路由路徑
path: '/content',
name: 'content',
//跳轉的組件
component: Content
},
{
//路由路徑
path: '/main',
name: 'main',
//跳轉的組件
component: Main
},
{
//路由路徑
path: '/kuang',
name: 'kuang',
//跳轉的組件
component: Kuang
}
]
})
Vue+Element
安裝
- 進入路徑,執行
vue init webpack xxxx
- 進入xxxx,執行
npm install --legacy-peer-deps [email protected]
- 安裝element-ui,
npm i element-ui -S
- 安裝依賴,
npm install
- 安裝SASS載入器,
cnpm install sass-loader node-sass --save-dev
- 啟動,進行測試,
npm run dev
sass-loader版本退回7.3.1
舉例
1.創建views目錄,創建視圖組件吧
Main.vue
<template>
<h1>首頁</h1>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped>
</style>
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>
2.創建路由
index.js
import Vue from "vue";
import Router from "vue-router";
import Main from "../views/Main";
import Login from "../views/Login";
Vue.use(Router);
export default new Router({
routes: [
{
path: '/main',
component: Main
},
{
path: '/login',
component: Login
}
]
});
3.main.js配置
main.js
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.use(router);
Vue.use(ElementUI)
new Vue({
el: '#app',
router,
render: h => h(App),//ElementUI規定這樣使用
})
4.App.vue中配置顯示視圖
<template>
<div id="app">
<router-link to="/login">login</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
}
</script>
路由嵌套
舉例
在之前的基礎上新增和修改對應頁面
Profile.vue
<template>
<h1>個人信息</h1>
</template>
<script>
export default {
name: "UserProfile"
}
</script>
<style scoped>
</style>
List.vue
<template>
<h1>用戶列表</h1>
</template>
<script>
export default {
name: "UserList"
}
</script>
<style scoped>
</style>
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>
index.js
import Vue from "vue";
import Router from "vue-router";
import Main from "../views/Main";
import Login from "../views/Login";
import UserList from "../views/user/List";
import UserProfile from "../views/user/Profile";
Vue.use(Router);
export default new Router({
routes: [
{
path: '/main',
component: Main,
//路由嵌套
children: [
{path: '/user/profile',component: UserProfile},
{path: '/user/list',component: UserList}
]
},
{
path: '/login',
component: Login
}
]
});