NPM:项目综合管理工具,类似与后端的maven
webpack:将es6打包成es5
第一个vue程序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
{{msg}}
</div>
<script>
var vm = new Vue({
el:"#app",
data:{
msg:"hello,vue!"
}
});
</script>
</body>
</html>
基本语法
v-bind(属性绑定)
vue不单止可以让html从model中取出数据,还可以让html标签在的属性拿到model层的数据。这就是v-bind。这种带v-xx的都是vue.js提供的指令
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<span v-bind:title="msg">鼠标悬停查看title属性值</span>
</div>
<script>
var vm = new Vue({
el:"#app",
data:{
msg:"hello,vue!"
}
});
</script>
</body>
</html>
v-if 和 v-else(条件判断)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<h1 v-if="ok">yes</h1>
<h1 v-else>no</h1>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
ok: true
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<ul>
<li v-for="item in items">
{{item.msg}}
</li>
</ul>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
items: [
{msg: "123"},
{msg: "321"}
]
}
});
</script>
</body>
</html>
v-on(事件)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<button v-on:click="sayHello">点击弹出</button>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: "hello"
},
methods: {
sayHello: function () {
alert(this.msg);
}
}
});
</script>
</body>
</html>
v-model(双向绑定)
v-model指令可以对表单控件实现双向绑定,也就是表单内容发生变化,model数据也跟着发生变化,model发生变化视图层内容也会更新,而且这种变化是实时进行的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
输入文本<input type="text" v-model="msg">{{msg}}
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
msg: ""
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<p>
<input type="radio" name="sex" v-model="sex" value="男"> 男
<input type="radio" name="sex" v-model="sex" value="女"> 女
{{sex}}
</p>
<p>
<select v-model="select">
<option disabled value="">请选择</option>
<option value="A">a</option>
<option value="B">b</option>
<option value="C">c</option>
</select>
{{select}}
</p>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
sex: "",
select: ""
}
});
</script>
</body>
</html>
Vue组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<name v-for="item in items" v-bind:arg="item"></name>
</div>
<script>
Vue.component("name", {
props: ['arg'],
template: '<li>{{arg}}</li>>'
});
var vm = new Vue({
el: "#app",
data: {
items:["java", "linux"]
}
});
</script>
</body>
</html>
Axios(vue中的异步通信)
axios使用es6语法,idea中需要将js语法设置成es6。加载ajax需要在钩子函数中加载,钩子函数是在vue生命周期中的某些阶段横插进去的一段方法。
第一种方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="vue">
<div>{{info}}</div>
</div>
<script>
var vm = new Vue({
el: "#vue",
data: {
info: null
},
mounted(){
axios.get('data.json').then(response=>(this.info=response.data));
}
});
</script>
</body>
</html>
第二种
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="vue">
<div>{{info}}</div>
<div>{{info.name}}</div>
</div>
<script>
var vm = new Vue({
el: "#vue",
data() {
return{
info: {
name: null,
url: null,
page: null,
isNonProfit: null,
address: {
street: null,
city: null,
country: null,
},
links: [
{
name: null,
url: null
},
{
name: null,
url: null
},
{
name: null,
url: null
},
]
}
}
},
mounted(){
axios.get('data.json').then(response=>(this.info=response.data));
}
});
</script>
</body>
</html>
axios携带参数
<script>
mounted(){
axios.get('data.json',{headers: {username: 'FELaoL3'}}).then(response=>(this.info=response.data));
}
});
</script>
vue闪烁问题
<head>
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div id="vue">
<div v-cloak>{{info}}</div>
</div>
将josn数据绑定到属性
<a v-bind:href="info.url">点击跳转</a>
计算属性
计算属性就是为了把不经常改变的属性缓存起来,需要时直接从内存中调用,而不必每次都调用函数,以达到节省系统开销的目的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<p>currentTime1 {{currentTime1()}}</p>
<p>currentTime2 {{currentTime2}}</p>
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
message: "hello"
},
methods: {
currentTime1: function () {
return Date.now();
}
},
computed: {
currentTime2: function () {
return Date.now();
}
}
});
</script>
</body>
</html>
slot插槽
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<todo>
<todo-title slot="slot-title" :title="title"></todo-title>
<todo-items slot="slot-items" v-for="item in items" :item="item"></todo-items>
</todo>
</div>
<script>
Vue.component("todo", {
template: '<div>\
<slot name="slot-title"></slot>\
<ul>\
<slot name="slot-items"></slot>\
</ul>\
</div>'
})
Vue.component("todo-title", {
props: ['title'],
template: '<div>{{title}}</div>'
})
Vue.component("todo-items", {
props: ['item'],
template: '<li>{{item}}</li>'
})
var vm = new Vue({
el: "#app",
data: {
title: "书籍列表",
items: ["java", "linux"]
}
});
</script>
</body>
</html>
v-on和自定义事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src='https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js'></script>
</head>
<body>
<div id="app">
<todo>
<todo-title slot="slot-title" :title="title"></todo-title>
<todo-items slot="slot-items" v-for="(item,index) in items" :index="index" :item="item" v-on:c-remove="Aremove"></todo-items>
</todo>
</div>
<script>
Vue.component("todo", {
template: '<div>\
<slot name="slot-title"></slot>\
<ul>\
<slot name="slot-items"></slot>\
</ul>\
</div>'
})
Vue.component("todo-title", {
props: ['title'],
template: '<div>{{title}}</div>'
})
Vue.component("todo-items", {
props: ['item', 'index'],
template: '<li>{{item}}<button @click="remove">删除</button></li>',
methods: {
remove: function (index){
this.$emit('c-remove');
}
}
})
var vm = new Vue({
el: "#app",
data: {
title: "书籍列表",
items: ["java", "linux"]
},
methods: {
Aremove: function (index) {
this.items.splice(index, 1);
}
}
});
</script>
</body>
</html>
第一个vue-cli
vue-cli就是一个脚手架,类似于我们的maven
它需要环境支持
确认node.js安装完毕
node -v 和npm -v 能够正确显示版本即代表安装完毕
安装npm淘宝镜像
npm install cnpm -g 全局安装淘宝镜像或者使用npm install --registry=https://registry.npm.taobao.org
淘宝镜像是一种备选,首选我们使用npm下载东西,只有当npm下载不了的时候,可以考虑使用淘宝镜像去下载,因为淘宝镜像下载的东西有可能在项目打包的时候会出现问题。
安装vue-cli
cnpm install vue-cli -g ,使用vue list 可以查看基于哪些模板创建vue程序
使用命令行创建第一个vue-cli
首先cd命令进入项目需要被创建的路径
然后执行vue init webpack 这里写项目名字
按照下列提示选择即可
project name:项目名字,初始化语句写过了,这里直接回车
project description:项目描述,直接回车
author:默认,直接回车
vue buid:vue编译方式,这里可以上下选择,我们直接选择第一个回车
剩下的都是选择no
初始化项目并运行
cd myvue
npm install
npm run dev
显示如下页面代表启动成功
就可以访问localhost8080端口了
ctrl+C停止项目
接下来就可以通过idea里的终端来打开项目了
webpack
它是一个模块加载器兼打包工具,它能把各种资源当作模块来处理和使用。这里我们主要使用它来把我们vue的es6语法代码打包成浏览器支持的es5
npm install webpack -g 和npm install webpack-cli -g 安装webpack
webpack -v 查看版本,由于我们生成的vue项目有指定webpack的版本号,但是这里安装的是最新的版本,可能会造成打包不成功,到时候需要将vue项目中指定的webpack版本号降低或者升高
使用
在指定路径创建一个新的文件夹,用idea打开它
项目根路径创建目录modules,然后新建hello。js和main。js
exports.sayHi= function () {
document.write("<h1>123</h1>")
};
var hello = require("./hello");
hello.sayHi();
项目根路径创建webpack配置文件webpack.config.js
module.exports = {
entry: './modules/main.js',
output: {
filename: "./js/bundle.js"
}
}
项目根路径创建项目入口index.html
<!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遵从Soc原则,只关注视图层,于是无法完成视图跳转的功能,vue-router专门被设计来完成视图跳转
安装
在idea项目中打开终端安装vue-router,npm install vue-router --save-dev 这个意思是安装vue-router,并且把它保存到开发配置环境中
如果安装失败,安装低版本npm install vue-router@3.2.0 --save-dev ,或者可能是npm版本问题,可以尝试npm install --legacy-peer-deps vue-router --save-dev
使用
1.在项目src目录下新建目录router
2.目录router下新建vue-router的配置文件index.js
import Vue from 'vue';
import VueRouter from 'vue-router';
import Content from "../components/Content";
import Main from "../components/Main";
Vue.use(VueRouter);
export default new VueRouter({
routes: [
{
path: '/content',
name: 'content',
component: Content
},
{
path: '/main',
name: 'main',
component: Main
}
]
})
3.在程序入口中配置路由
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/>'
})
4.在项目入口中配置路由
<template>
<div id="app">
<h1>123</h1>
<!--使用vue路由的跳转方式,弃用a标签-->
<router-link to="/main">首页</router-link>
<router-link to="/content">内容页</router-link>
<router-view></router-view><!--显示视图-->
</div>
</template>
<script>
/*导入组件*/
import Content from "./components/Content";
import Main from "./components/Main";
export default {
name: 'App',
/*在绑定的元素中显示声明组件*/
components: {
Content,
Main
}
}
</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使用
初始化项目
首先,我们先使用webpack初始化一个vue项目vue init webpack hello-vue ,然后cd进入这个目录
npm install vue-router@3.2.0 --save-dev
npm i element-ui -S
npm install
cnpm install sass-loader node-sass --save-dev
npm run dev
测试正常之后就可以使用idea打开了
在src下新建两个目录router和views用来做路由和视图层组件
router目录下的路由配置index.js
import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login";
import Main from "../views/Main";
Vue.use(VueRouter);
export default new VueRouter({
routes: [
{
path: '/main',
component: Main
},
{
path: '/login',
component: Login
}
]
});
视图层组件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>
程序入口配置main.js
import Vue from 'vue'
import App from './App'
import router from './router';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.config.productionTip = false
Vue.use(router);
Vue.use(ElementUI);
new Vue({
el: '#app',
router,
render: h => h(App)
})
项目入口app.vue
<template>
<div id="app">
<!--路由显示页面-->
<router-view></router-view>
</div>
</template>
<script>
到这里直接运行,如果报错sass版本不兼容的话
sass不兼容
方式一:
查看自己的node版本,如果是16的版本,安装sass6版本
去修改package.json文件里的sass-loader版本为6.x,然后运行安装npm install 如果安装失败,使用cnpm安装
方式二:
狠一点,它不是说版本不兼容嘛,直接降级到它指定的低版本4.0.0
还是一样修改package.json文件里的sass-loader版本为4.0.0,然后运行npm install,报错运行cnpm install即可
路由嵌套
路由嵌套就是子路由
主页面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: #048bd1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
user目录下
<template>
<h1>用户列表</h1>
</template>
<script>
export default {
name: "UserList"
}
</script>
<style scoped>
</style>
在main路由中注册子路由
import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login";
import Main from "../views/Main";
import UserList from '../views/user/List';
import UserProfile from "../views/user/Profile";
Vue.use(VueRouter);
export default new VueRouter({
routes: [
{
path: '/main',
component: Main,
children: [
{path: '/user/profile', component: UserProfile},
{path: '/user/list', component: UserList},
]
},
{
path: '/login',
component: Login
}
]
});
参数传递和重定向
参数传递
跳转设置
<!--to属性需要使用单向绑定 name到路由上的一个定义号的组件名字,带上参数params-->
<router-link :to="{name: 'UserProfile', params: {id: 1} }">个人信息</router-link>
路由设置
export default new VueRouter({
routes: [
{
path: '/main',
component: Main,
children: [
{path: '/user/profile/:id', name: 'UserProfile', component: UserProfile, props: true},
{path: '/user/list', component: UserList},
]
},
{
path: '/login',
component: Login
}
]
});
组件设置
<template>
<div>
<!--取出js中的参数-->
{{id}}
</div>
</template>
<script>
export default {
/*从路由中接收参数*/
props: ['id'],
name: "UserProfile"
}
</script>
重定向
index.js
{
path: '/goHome',
redirect: '/main'
}
404和路由钩子
路由模式
hash模式:url会带上#/
history:url不显示#
在路由配置index.js中配置mode: ‘history’,
404
配置路由即可
{
path: '*',
component: NotFound
}
路由钩子
beforeRouteEnter:进入路由前执行
beforeRouteLeave:离开路由前执行
export default {
props: ['id'],
name: "UserProfile",
beforeRouteEnter: (to, from, next)=>{
alert("进入路由之前");
next();
},
beforeRouteLeave: (to, from, next)=>{
alert("离开路由之前");
next();
}
}
next还可以带上参数
next(’/path‘)改变路由跳转方向,使其跳转到其他路由
next(false)返回原来的页面
调用vm对象
next(vm=> {
vm.getData();
});
安装axios
npm install axios -s
npm install --save vue-axios
在static目录新建资源测试目录mock,创建数据测试文件data.json
导入axios,main.js
import Vue from 'vue'
import App from './App'
import router from './router';
import axios from "axios";
import VueAxios from 'vue-axios';
Vue.config.productionTip = false
Vue.use(VueAxios, axios)
Vue.use(router);
new Vue({
el: '#app',
router,
render: h => h(App)
})
配置组件路由钩子
<template>
<div>
<h1>个人信息</h1>
<!--取出js中的参数-->
{{id}}
</div>
</template>
<script>
export default {
/*从路由中接收参数*/
props: ['id'],
name: "UserProfile",
/*参数和filter过滤器类似,to相当于request,from相当于respond, next相当于chain*/
beforeRouteEnter: (to, from, next)=>{
alert("进入路由之前");
next(vm => {
vm.getData();
});
},
beforeRouteLeave: (to, from, next)=>{
alert("离开路由之前");
next();
},
methods: {
getData: function () {
this.axios({
method: 'get',
url: 'http://localhost:8080/static/mock/data.json'
}).then(function (response) {
console.log(response);
})
}
}
}
</script>
<style scoped>
</style>
|