知识点:hello world 结构
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="root">div>
<script>
Vue.createApp({
template: 'hello world'
}).mount("#root");
script>
body>
html>
知识点: 数据绑定
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="root">div>
<script>
Vue.createApp({
data() {
return {
count: 1
}
},
template: '{
{count}}',
mounted() {
setInterval(() => {
this.count++;
}, 1000);
}
}).mount("#root");
script>
body>
html>
知识点: v-on 监听事件
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="root">div>
<script>
Vue.createApp({
data() {
return {
content: 'hello world'
}
},
methods: {
handleReverse() {
this.content = this.content.split('').reverse().join('');
}
},
template: `
{
{content}}
`
}).mount("#root");
script>
body>
html>
知识点:v-model 双向绑定,v-for 循环
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="root">div>
<script>
Vue.createApp({
data() {
return {
inputValue: '',
list: []
}
},
methods: {
handleAddItem() {
this.list.push(this.inputValue);
this.inputValue = '';
}
},
template: `
- {
{item}} {
{index}}
`
}).mount("#root");
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
head>
<body>
<div id="root">div>
<script>
const app = Vue.createApp({
data() {
return {
inputValue: '',
list: []
}
},
methods: {
handleAddItem() {
this.list.push(this.inputValue);
this.inputValue = '';
}
},
template: `
`
});
app.component('todo-item', {
props: ['item', 'index'],
template: '{
{index}}--{
{item}} '
});
app.mount("#root");
script>
body>
html>
// createApp 表示创建一个 Vue 应用,存储到 app 变量中
// 传入的参数表示,这个应用最外层的组件,应该如何展示
// mvvm 设计模式, m -> model 数据,v -> view 视图,vm -> viewModel 视图数据连接层
const app = Vue.createApp({
data() {
return {
message: 'hello world'
}
},
template: '{
{message}}';
});
// vm 代表的就是 vue 应用的根组件
const vm = app.mount('#root');
// vm.$data.message
// 生命周期函数:在某一时刻会自动执行的函数
const app = Vue.createApp({
data() {
return {
message: 'hello world'
}
},
// 在实例生成之前会自动执行的函数
beforeCreate() {
console.log('beforeCreate');
},
// 在实例生成之后会自动执行的函数
created() {
console.log('created');
},
// 在模板已经被编程函数之后立即自动执行的函数
// 在组件内容被渲染到页面之前自动执行的函数
beforeMount() {
console.log('beforeMount');
},
// 在组件内容被渲染到页面之后自动执行的函数
mounted() {
console.log('mounted');
},
// 当 data 中的数据发生变化时会自动执行的函数
beforeUpdate() {
console.log('beforeUpdate');
},
// 当 data 中的数据发生变化,同时页面完成更新后,会自动执行的函数
updated() {
console.log('updated');
},
// 当 Vue 应用失效时,自动执行的函数
beforeUnmount() {
console.log('beforeUnmount');
},
// 当 vue 应用失效时,且 dom 完全销毁之后,自动执行的函数
unmounted() {
console.log('unmounted');
}
});
const app = Vue.createApp({
data() {
return {
message: 'hello world'
}
},
tempate: '';
});
const vm = app.mount('#root');
const app = Vue.createApp({
data() {
return {
message: 'hello world'
}
},
template: '{
{message}}'
});
const vm = app.mount('#root');
const app = Vue.createApp({
data() {
return {
message: 'hello world',
show: true
}
},
template: '{
{message}}'
});
const vm = app.mount('#root');
const app = Vue.createApp({
data() {
return {
message: 'hello world'
}
},
methods: {
handleClick() {
alert('click');
}
},
template: '{
{message}}'
});
const vm = app.mount('#root');
const app = Vue.createApp({
data() {
return {
message: 'hello world'
}
},
methods: {
handleClick() {
alert('click');
}
},
template: '{
{message}}'
});
const vm = app.mount('#root');
动态属性可以在属性变量上加 [] 中括号来实现
const app = Vue.createApp({
data() {
return {
message: 'hello world',
name: 'title',
event: 'click'
}
},
methods: {
handleClick() {
alert('click');
}
},
template: '{
{message}}'
});
const vm = app.mount('#root');
const app = Vue.createApp({
data() {
return {
message: 'hello world',
name: 'title',
event: 'click'
}
},
methods: {
handleClick() {
alert('click');
}
},
template: `
`
});
const vm = app.mount('#root');
const app = Vue.createApp({
data() {
return {
message: 'hello world',
count: 2,
price: 5
}
},
// 当计算属性依赖的内容发生变更时,才会重新执行
computed: {
total() {
return this.count * this.price;
}
},
methods: {
// 只要页面重新渲染,就会重新计算
getTotal() {
return this.count * this.price;
}
},
template: `{
{total}}`
});
const vm = app.mount('#root');
const app = Vue.createApp({
data() {
return {
message: 'hello world',
count: 2,
price: 5,
newTotal: 0,
}
},
watch: {
// price 发生变化时,函数会执行
price() {
this.newTotal = this.price * this.count;
}
},
computed: {
// 当计算属性依赖的内容发生变更时,才会重新执行计算
total() {
return this.count * this.price;
}
},
template: `{
{newTotal}}`
});
const vm = app.mount('#root');
computed 和 method 都能实现的一个功能,建议使用 computed,因为有缓存
computed 和 watcher 都能实现的功能,建议使用 computed 因为更加简洁
const app = createApp({
data: {
classString: 'red',
classObject: {
red: false, green: true},
classArr: ['red', 'green', {
brown: true}]
},
template: `
`
});
const vm = app.mount('#root');
const app = createApp({
template: `
hello world
`
});
app.component('demo', {
template: `
one
two
`;
});
const vm = app.mount('#root');
const app = Vue.createApp({
data: {
styleString: 'color: yellow',
styleObject: {
color: 'yellow'
},
},
template: 'hello world';
});
const vm = app.mount('root');
如果频繁显示隐藏,用 v-show,避免频繁的销毁创建元素
...
template: `
`;
...
template: `
if
else if
else
`;
const app = Vue.createApp({
data: {
listArray: ['first', 'second', 'thrid'],
listObject: {
first: 1,
second: 2,
thrid: 3
}
},
template: `
{
{item}}
{
{item}} -- {
{index}}
{
{value}} -- {
{key}}
{
{value}} == {
{key}} -- {
{index}}
`
});
template: `
{
{item}} -- {
{index}}
`
数组
this.listArray = ['bye', 'world'];
this.listArray = ['bye'].concat(['world']);
this.listArray = ['bye', 'world'].filter(item => item === 'bye');
this.listArray[1] = 'hello';
对象
直接添加对象的内容,也可以自动的展示出来
this.listObject.age = 18;
直接循环数字
template: `
{
{item}}
` // 会直接循环从 1 到 10 数字
如果再循环的过程中想要加判断
template: `
{
{value}} -- {key}
`
template: `
{
{value}} -- {
{key}}
`;
data() {
return {
counter: 0
}
},
methods: {
handleBtnClick(num, $event) {
this.counter += num;
}
},
template: '',
template: `
`
stop 阻止事件冒泡
self 判断点击的为自己
prevent 阻止默认行为
capture 会把事件运营模式变成的捕获模式
once 事件绑定只执行一次
passive 在事件为 scroll 时,会提升性能
template: `
`
enter,tab,delete,esc,up,down,left,right
template: `
`
left,right,middle
exact
// 在按住 ctrl 并且没有按住其他键时,点击触发
template: `
123
`
template: `
`
template: `
`
const app = Vue.createApp({
data() {
return {
message: true
}
},
template: `
`
});
const app = Vue.createApp({
data() {
return {
message: []
}
},
template: `
`
});
checkbox 修改 true,false 选中的值
const app = Vue.createApp({
data() {
return {
message: 'world'
}
},
template: `
`
});
const app = Vue.createApp({
data() {
return {
message: ''
}
},
template: `
`
});
const app = Vue.createApp({
data() {
return {
message: ''
}
},
template: `
`
});
const app = Vue.createApp({
data() {
return {
message: []
}
},
template: `
`
});
const app = Vue.createApp({
data() {
return {
message: 'hello'
}
},
template: `
`
});
const app = Vue.createApp({
data() {
return {
message: 123
}
},
template: `
`
});
trim
去掉前后的空格
全局组件
app.component('hello-world', {
template: 'hello world'
});
局部组件
const HelloWorld = {
template: 'hello world'
}
const app = Vue.createApp({
components: {
// 'hello-world': HelloWorld
HelloWorld
},
template: `
`
});
const app = Vue.createApp({
template: `
`
});
app.component('test', {
props: ['content'],
template: '{
{content}}'
});
如果使用静态传参,那么只能传字符串类型,而动态传参可以传数值类型等
const app = Vue.createApp({
data() {
return {
num: 123
}
},
template: `
`
});
app.component('test', {
props: ['content'],
template: `
{
{typeof content}}
`
});
const vm = app.mount('#root');
type: String,Boolean,Array,Object,Function,Symbol
required:必填
default:默认值
app.component('test', {
props: {
content: Number
}
template: `
{
{typeof content}}
`
});
app.component('test', {
props: {
content: {
type: Number,
// required: true,
default: 789
}
},
template: `{
{content}}`
});
对传过来的值做更为深层次的校验
app.component('test', {
props: {
type: Number,
validator: function (value) {
return value < 1000;
},
default: function() {
return 456;
}
},
template: `{
{content}}`
});
绑定较多变量时的简写方式
v-bind=“params” 等价于 :a=“params.a” :b=“params.b” :c=“params.c”
const app = Vue.createApp({
data() {
return {
params: {
a: '123',
b: '456',
c: '789'
}
}
},
template: ` `
});
app.component('test', {
props: ['a', 'b', 'c'],
template: `
{
{a}} -- {
{b}} -- {
{c}}
`
});
const app = Vue.createApp({
template: `
`
});
app.component('counter', {
mounted() {
console.log(this.$attrs);
},
// inhertAttrs : false,
template: `
Counter
Counter
Counter
`
});
const app = Vue.createApp({
data() {
return {
count: 1
}
},
methods: {
handleAdd(step) {
this.count += step;
}
},
template: `
`
});
app.component('counter', {
props: ['count'],
methods: {
handleClick() {
this.$emit('add', 2);
}
},
template: `
{
{count}}
`,
});
app.mount("#root");
app.component('counter', {
props: ['count'],
emits: ['add'],
methods: {
handleClick() {
this.$emit('add', this.count + 3);
}
},
template: `
{
{count}}
`
});
app.component('counter', {
props: ['count'],
emits: [{
add: (count) => {
if (count > 0) {
return true;
}
return false;
}
}],
methods: {
handleClick() {
this.$emit('add', this.count + 3);
}
},
template: `
{
{count}}
`
});
const app = Vue.createApp({
data() {
return {
count: 1
}
},
template: ' '
});
app.component('counter', {
props: ['modelValue'],
methods: {
handleClick() {
this.$emit('update:modelValue', this.modelValue + 3);
}
},
template: '{
{modelValue}}'
});
app.mount('#root');
const app = Vue.createApp({
data() {
return {
count: 'a'
}
},
template: `
`
});
app.component('counter', {
props: {
'modelValue': String,
'modelModifiers': {
default: () => ({
})
}
},
methods: {
handleClick() {
let newValue = this.modelValue + 'b';
if (this.modelModifiers.uppercase) {
newValue = newValue.toUpperCase();
}
this.$emit('update:modelValue', newValue);
}
},
template: `
{
{modelValue}}
`,
});
app.mount("#root");
const app = Vue.createApp({
data() {
return {
text: '提交'
}
},
template: `
{
{text}}
`
});
app.component('myform', {
template: `
default value
`,
});
app.mount("#root");
const app = Vue.createApp({
template: `
header
footer
`
});
app.component('layout', {
template: `
content
`,
});
app.mount("#root");
v-slot:header 可简化为 #header
const app = Vue.createApp({
template: `
header
footer
`
});
const app = Vue.createApp({
// {item} 为解构
template: `
{
{item}}
`
});
app.component('list', {
data() {
return {
list: [1,2,3]
}
},
template: `
`,
});
app.mount("#root");
const app = Vue.createApp({
data() {
return {
currentItem: 'input-item'
}
},
methods: {
handleClick() {
if (this.currentItem === 'input-item') {
this.currentItem = 'common-item';
} else {
this.currentItem = 'input-item';
}
}
},
template: `
`
});
app.component('input-item', {
template: ''
});
app.component('common-item', {
template: 'hello world'
});
app.mount("#root");
const app = Vue.createApp({
template: `
`
});
app.component('common-item', {
template: `
hello world
`,
});
app.component('async-common-item', Vue.defineAsyncComponent(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
template: 'this is an async component'
});
}, 4000);
});
}));
app.mount("#root");
const app = Vue.createApp({
data() {
return {
count: 1
}
},
mounted() {
this.$ref.count.innerHTML = 'hello';
},
template: `
{
{count}}
`
});
const app = Vue.createApp({
data() {
return {
count: 1
}
},
mounted() {
this.$ref.common.sayHello();
},
template: `
`
});
app.component('common-item', {
methods: {
sayHello() {
alert('hello');
}
},
template: ``
});
const app = Vue.createApp({
provide: {
count: 1
}
...
});
app.component('child', {
...
});
app.component('child-child', {
inject: ['count']
});
const app = Vue.createApp({
data() {
return {
count: 1
}
},
provide() {
return {
count: this.count
};
}
...
});
app.component('child', {
...
});
app.component('child-child', {
inject: ['count']
});
使用
v-enter-from 开始进入
v-enter-active 进入过程
v-enter-to 进入结束
v-leave-from 开始离开
v-leave-active 离开过程
v-leave-to 离开结束
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
<style>
.v-enter-from {
opacity: 0;
}
.v-enter-active, .v-leave-active {
transition: opacity 2s;
}
.v-enter-to {
opacity: 1;
}
.v-leave-to {
opacity: 0;
}
style>
head>
<body>
<div id="root">div>
<script>
const app = Vue.createApp({
data() {
return {
show: false
}
},
template: `
hello world
`
});
app.mount('#root');
script>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
<style>
.com-enter-from {
opacity: 0;
}
.com-enter-active, .com-leave-active {
transition: opacity 2s;
}
.com-enter-to {
opacity: 1;
}
.com-leave-to {
opacity: 0;
}
style>
head>
<body>
<div id="root">div>
<script>
const app = Vue.createApp({
data() {
return {
show: false
}
},
template: `
hello world
`
});
app.mount('#root');
script>
body>
html>
template: `
hello world
`
template: `
hello world
`
template: `
hello world
`
template: `
hello world
`
template: `
hello world
`
const app = Vue.createApp({
data() {
return {
show: false
}
},
methods: {
handleBeforeEnter(el) {
el.style.color = "red";
},
handleEnter(el, done) {
const timer = setInterval(() => {
const color = el.style.color;
if (color === 'red') {
el.style.color = 'blue';
} else {
el.style.color = 'red';
}
}, 1000);
setTimeout(() => {
clearInterval(timer);
done();
}, 3000)
},
handleEnterEnd() {
alert(111);
}
},
template: `
hello world!
`
});
app.mount('#root');
template: `
hello world
bye world
`
template: `
hello world
bye world
`
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
<style>
.v-leave-to,
.v-enter-from {
opacity: 0;
}
.v-leave-active,
.v-enter-active {
transition: opacity 2s ease 0s;
}
.v-leave-from,
.v-enter-to {
opacity: 1;
}
style>
head>
<body>
<div id="root">div>
<script>
const componentA = {
template: 'hello world'
};
const componentB = {
template: 'bye world'
};
const app = Vue.createApp({
components: {
'component-a': componentA,
'component-b': componentB
},
data() {
return {
currentComponent: 'component-a'
}
},
methods: {
handleClick() {
if (this.currentComponent === 'component-a') {
this.currentComponent = 'component-b';
} else {
this.currentComponent = 'component-a';
}
}
},
template: `
`
});
app.mount('#root');
script>
body>
html>
.v-move 列表中移动的项的移动渐变动画
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<script src="https://unpkg.com/vue@next">script>
<style>
.list-item {
display: inline-block;
margin-left: 10px;
}
.v-enter-from {
opacity: 0;
transform: translateY(20px);
}
.v-enter-active {
transition: all 2s ease 0s;
}
.v-enter-to {
opacity: 1;
transform: translateY(0);
}
.v-move {
transition: all 2s ease 0s;
}
style>
head>
<body>
<div id="root">div>
<script>
const app = Vue.createApp({
data() {
return {
list: [1, 2, 3]
}
},
methods: {
handleClick() {
this.list.unshift(this.list.length + 1);
}
},
template: `
{
{item}}
`
});
app.mount('#root');
script>
body>
html>