前端浅学之Vue
浅学Vue
引入
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
Hello Vue
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/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/vue@2.5.21/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/vue@2.5.21/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/vue@2.5.21/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/vue@2.5.21/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/vue@2.5.21/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/vue@2.5.21/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/vue@2.5.21/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/vue@2.5.21/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 vue-router@3.5.2
例子
.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 vue-router@3.5.2
- 安装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
}
]
});