v-once : 只渲染一次,不会随着数据的改变而改变
v-html : 会将string的HTML解析出来并且渲染
v-text : 接受一个string类型,将数据显示在界面中
v-pre : 跳过这个元素和它子元素的编译过程,用于显示本地的Mustache语法
v-bind:
作用:动态绑定属性
缩写为 : (语法糖的形式)
v-bind动态绑定class
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>classtitle>
head>
<style>
.active {
color: red;
}
style>
<body>
<div id="app">
<h2 :class="active">{{ message }}h2>
<h2 :class="{active: isActive}">{{ message }}h2>
div>
body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el : '#app',
data: {
message: "你好",
active: 'active',
isActive : true
}
})
script>
html>
v-bind动态绑定style
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>style动态绑定title>
head>
<body>
<div id="app">
<h2 :style="{'font-size' : finalSize}">{{ message }}h2>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el : '#app',
data: {
message: "你好",
finalSize: '100px'
}
})
script>
body>
html>
v-on:事件监听,比如点击,拖拽,键盘事件等等
作用:绑定事件监听器
缩写 @ (语法糖的形式)
参数: event
修饰符:Vue提供了修饰符来处理一些事件
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修饰符title>
head>
<body>
<div id="app">
<div @click="divClick">
aaaaa
<button @click.stop = "btnClick">点击button>
div>
<form action="baidu">
<input type="submit" value="提交" @click.prevent = "subClick"/>
form>
<input type="text" @keyup.enter="keyup">
<button @click.once="onceClick">按钮button>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el: "#app",
data: {
message: '你好呀'
},
methods: {
btnClick() {
console.log("btnClick");
},
divClick() {
console.log("divClick");
},
subClick() {
console.log("subClick");
},
keyup() {
console.log("keyup");
},
onceClick() {
console.log("onceClick");
}
}
})
script>
body>
html>
doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>计算属性title>
head>
<body>
<div id="app">
<h5>{{ fullName }}h5>
<h5>{{ fullName }}h5>
<h5>{{ fullName }}h5>
<h5>{{ fullName }}h5>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el: '#app',
data: {
firsrName: '阿修罗',
lastName: '剑圣'
},
computed: {
fullName: function () {
return this.firsrName + " " + this.lastName
}
}
})
script>
body>
html>
setter和getter
<script>
const app = new Vue({
el: '#app',
data: {
firsrName: '阿修罗',
lastName: '剑圣'
},
computed: {
// fullName: function () {
// return this.firsrName + " " + this.lastName
// }
fullName: {
set: function () {
},
get: function () {
return this.firsrName + " " + this.lastName
}
}
}
})
script>
切换类型发现文字依然显示之前的输入的内容.
问题解答:因为在Vue进行渲染时,出于性能考虑,会尽可能的复用已经存在的元素.而不是创建新的元素
解决方案:不希望使用Vue出现类似重复使用的问题,可以给对应的input添加key,并且需要保持key的不同
开发中如何选择:
const nums = [10, 20, 30, 50, 666, 7]
let newNums = nums.filter(function (n) {
return n < 100
})
console.log(newNums)
对数组的内容进行汇总
v-model其实是一个语法糖,它的背后本质包含两个操作:
<input type="text" v-model="message">
<input type="text" v-bind="message" v-on:input="message = $event.target.value">
lazy修饰符
number修饰符
trim修饰符
组件化的基本使用
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件的使用title>
head>
<body>
<div id="app">
<my>my>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
// 创建组件构造器
const myComponent = Vue.extend({
template:
`
组件标题
组件中的一段内容
`
});
// 注册组件
Vue.component('my', myComponent);
const app = new Vue({
el: "#app"
})
script>
body>
html>
<script>
const comC1 = Vue.extend({
template:
`
阿修罗
今天的天气很好
`
});
const comC2 = Vue.extend({
template:
`
子组件
子组件注册完成
`,
components: {
com1: comC1
}
});
const app = new Vue({
el: "#app",
components: {
com2: comC2
}
})
</script>
<script>
const comC2 = Vue.extend({
template:
`
子组件
子组件注册完成
`,
components: {
"com1": {
template:
`
阿修罗
今天的天气很好
`
}
}
});
const app = new Vue({
el: "#app",
components: {
com2: comC2
}
})
script>
父子组件之间的通信:
props基本用法:
props的值有两种方式:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父组件向子组件传递数据title>
head>
<body>
<div id="app">
<tem v-bind:cmessage="message" :cmovies="movies">tem>
div>
<template id="tem">
<div>
<h2>{{ cmessage }}h2>
<ul>
<li v-for="movie in cmovies">{{ movie }}li>
ul>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el: "#app",
data: {
message: "阿修罗",
movies: ["异星灾难", "三体", "权利的游戏"]
},
components: {
"tem" : {
template: "#tem",
// 以数组的方式传递
// props: ["cmessage", "cmovies"]
props: {
// 类型限制
// cmessage: String,
// cmovies: Array
// 提供一些默认值
cmessage: {
type: String,
default: "没有传值",
required: true
},
cmovies: {
type: Array,
default: ["西游记", "三国演义", "水浒传"],
required: true
}
}
}
}
})
script>
body>
html>
子组件向父组件传递数据:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>子组件向父组件传递数据title>
head>
<body>
<div id="app">
<cpn @itemclick="cpnClick">cpn>
div>
<template id="cpn">
<div>
<button v-for="item in categories"
@click="btnClick(item)">
{{ item.name }}
button>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const cpn = {
template: "#cpn",
data() {
return {
categories: [
{id: "aaa", name: "热门推荐"},
{id: "bbb", name: "手机数码"},
{id: "ccc", name: "家用家电"},
{id: "ddd", name: "电脑办公"}
]
}
},
methods: {
btnClick(item) {
this.$emit('itemclick', item)
}
}
}
const app = new Vue({
el: "#app",
data: {
},
methods: {
cpnClick(item) {
console.log("cpnClick", item);
}
},
components: {
cpn
}
})
script>
body>
html>
访问方式:
父组件访问子组件: 使用$children或者是refs
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>父组件访问子组件title>
head>
<body>
<div id="app">
<cpn ref="aaa">cpn>
<cpn ref="cpn">cpn>
<button @click="btnClick">点击出现button>
div>
<template id="tem">
<div>我是子组件div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el: "#app",
methods: {
btnClick() {
// console.log(this.$children);
// console.log(this.$children[0].showMessage);
console.log(this.$refs.aaa);
}
},
components: {
cpn: {
template: "#tem",
methods: {
showMessage() {
console.log("showMessage");
}
}
}
}
})
script>
body>
html>
子组件访问父组件: 使用$parent(不建议使用)
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<cpn>cpn>
<cpn><button>新加按钮button>cpn>
<cpn><span>天气span>cpn>
<cpn>
<div>阿修罗div>
cpn>
div>
<template id="tem">
<div>
<h2>哈哈h2>
<slot><button>默认按钮button>slot>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el: "#app",
components: {
cpn: {
template: "#tem"
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<cpn><span slot="right">替换右边span>cpn>
<cpn><span slot="center">替换中间span>cpn>
div>
<template id="tem">
<div>
<slot name="left">左边slot>
<slot name="center">中间slot>
<slot name="right">右边slot>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el: "#app",
components: {
cpn: {
template: "#tem"
}
}
})
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<div id="app">
<cpn>
<template slot-scope="slot">
<span>{{ slot.data.join("-")}}span>
template>
cpn>
<cpn>
<template slot-scope="slot">
<span>{{ slot.data.join("*")}}span>
template>
cpn>
div>
<template id="tem">
<div>
<slot :data="pLanguage">
<ul>
<li v-for="item in pLanguage">{{ item }}li>
ul>
slot>
div>
template>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const app = new Vue({
el: '#app',
components: {
cpn: {
template: '#tem',
data() {
return {
pLanguage: ["JavaScript", "C#", "java", "Go"]
}
}
}
}
})
script>
body>
html>
npm管理node下的各种包
webpack用来做什么?
loader的使用过程:
使用步骤:
runtime-compiler的渲染过程:
template -> ast -> render -> vdom -> UI
runtime-only的渲染过程:
render -> vdom -> UI
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>箭头函数title>
head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
// 参数问题
// 两个参数
const sum = (num1, num2) => {
return num2 + num1
}
// 一个参数
const power = num => {
return num * num
}
// 返回值问题
// 函数代码块中只有一行代码
const mul = (num1, num2) => num1 * num2
console.log(mul(20, 30));
const demo = () => console.log("Hello World")
console.log(demo());
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>this的使用title>
head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
const obj = {
aaa() {
setTimeout(function () {
setTimeout(function (){
console.log(this) // this
})
setTimeout(() => {
console.log(this) // this
})
})
setTimeout(() => {
setTimeout(function () {
console.log(this) // this
})
setTimeout(() => {
console.log(this) // obj
})
})
}
}
obj.aaa()
script>
body>
html>
步骤一:安装vue-router
npm install vue-router --save
步骤二:
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
步骤三:
router-link的一些其他属性:
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
// 重定向
redirect: '/Home'
},
{
path: '/home',
name: 'Home',
// 路由的懒加载
component: () => import("../components/Home")
},
{
path: '/about',
name: 'About',
// 路由懒加载
component: () => import("../components/About")
},
{
path: '/user/:userId',
name: 'User',
// 路由懒加载
component: () => import("../components/User")
}
],
// 将默认的hash模式改为history模式
mode: 'history'
})
params类型
query类型
axios.all([axios({
url: ""
}),axios({
url: "",
params: {
}
})]).then(res => {
})
方式一
export function request(config) {
return new Promise((resolve, reject) => {
const instance = axios.create({
baseURL: "",
timeout: 5000
})
return instance(config)
})
}
方式二:
export function request(config) {
return new Promise((resolve, reject) => {
const instance = axios.create({
baseURL: "",
timeout: 5000
})
instance(config)
.then(res => {
resolve(res)
})
.catch(err => {
reject(err)
})
})
}
什么情况下可以用到?
一般情况下有异步操作时,使用Promise对操作进行封装
new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 2000)
}).then(() => {
console.log("hello world");
console.log("hello world");
console.log("hello world");
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 2000)
}).then(() => {
console.log("hello world");
console.log("hello world");
console.log("hello world");
})
})
Promise.all([
new Promise((resolve, reject) => {
setTimeout(() => {
resolve({name: "张三", age: 18})
}, 2000)
}),
new Promise((resolve, reject) => {
setTimeout(() => {
resolve({name: "kobe", age: 19})
}, 1000)
})
]).then(results => {
console.log(results);
})
import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
counter: 1000
},
mutations: {
// 定义方法
increment(state) {
state.counter ++
},
decrement(state) {
state.counter --
},
// mulations特殊的提交风格
incrementCount(state, payload) {
state.counter += payload.count
}
},
actions: {},
getters: {},
modules: {}
})
export default store
{{ $store.state.counter }}
import Vue from "vue"
import Vuex from "vuex"
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
counter: 1000,
students: [
{id: 1001, name: "hary", age: 18},
{id: 1002, name: "why", age: 19},
{id: 1003, name: "koba", age: 20},
{id: 1003, name: "curry", age: 21}
]
},
mutations: {
// 定义方法
increment(state) {
state.counter ++
},
decrement(state) {
state.counter --
}
},
actions: {},
getters: {
powerCounter(state) {
return state.counter * state.counter
},
getAgeMore20Stu(state) {
// return state.students.filter((s) => {
// return s.age > 20
// })
return state.students.filter((s => s.age > 20))
},
getStuLength(state, getters) {
return getters.getAgeMore20Stu.length
}
},
modules: {}
})
export default store