Home > Article > Web Front-end > Knowledge points gained using Vue3.0 (1)

Relevant learning recommendations: javascript video tutorial
Front-end development is full of flowers, and a skill is not yet mature, but a hundred skills are born. I’m at a loss and don’t know where to start. Follow the editor for more information.
I feel very busy at work recently, and I don’t have much time to write articles. Today’s article is mainly about some notes I compiled during my early study of Vue3.0 To summarize, by reading this article, you will be able to complete the Vue3.0 environment setup by yourself, and at the same time, you will also understand some of the new features of Vue3.0 to facilitate your own developmentVue3.0 learning. This article was first published on the public account [Front-end Youyouwan]. Follow === for a while. There are more interview questions waiting for you.
All examples in this article are implemented using
ant design vue2.0. For information aboutant design vue2.0, please refer to 2x.antdv.com/docs/ vue/in…
In the previous article, we built a development environment through vite, but in fact nowvite is not perfect enough to support a complete project, so in this article we still choose to use vue-cli scaffolding to build the environment.
The
vue-cliversion used by the editor is4.5.4. If your version is older, you can passnpm update @vue/ clito upgrade the scaffolding version. If it is not installed, you can install it throughnpm install @vue/cli -g
Open the terminal (cmd) in the workspace, and then initialize the project through the vue create my-vue3-test command
in The first step is to select Manually select features to manually select the features
and then select <pre class="brush:php;toolbar:false;">Choose Vue version
Babel
TypeScript
Router
Vuex
CSS Pre-processors
Linter/Formatter复制代码</pre># through Space
version, select 3.x(Preview)
Select n, that is, enter n and press Enter
y `
Inputn
Preprocessor selectionLess
Select ESLint Prettier
and In dedicater config files
cd my-vue3-test, and then execute yarn serve to start the project
http://localhost:8080/Access project
Vue3.0 has not yet been released, the relatively famous front-end in China# The ##UI library is the first to inherit Vue3.0 into its own UI library. The PC end is mainly ant-design -vue, the mobile version is mainly vant, all sample codes in this article will be based on ant-design-vue, first we install ant-design-vue
yarn add ant-design-vue@2.0.0-beta.6 yarn add babel-plugin-import -D复制代码
Load on demandEnter the project root directory, then open the
file, and modify the content inside to <pre class="brush:php;toolbar:false;">module.exports = { presets: ["@vue/cli-plugin-babel/preset"], plugins: [ // 按需加载
[ "import", // style 为 true 加载 less文件
{ libraryName: "ant-design-vue", libraryDirectory: "es", style: "css" }
]
]
};复制代码</pre>
To add a small page, we directly replace the code in the views/Home.vue file with <pre class="brush:php;toolbar:false;"><template>
<a-form layout="inline" :model="state.form">
<a-form-item>
<a-input v-model:value="state.form.user" placeholder="Username">
<template v-slot:prefix
><UserOutlined style="color:rgba(0,0,0,.25)"
/></template>
</a-input>
</a-form-item>
<a-form-item>
<a-input
v-model:value="state.form.password"
type="password"
placeholder="Password"
>
<template v-slot:prefix
><LockOutlined style="color:rgba(0,0,0,.25)"
/></template>
</a-input>
</a-form-item>
<a-form-item>
<a-button
type="primary"
:disabled="state.form.user === &#39;&#39; || state.form.password === &#39;&#39;"
@click="handleSubmit"
>
登录 </a-button>
</a-form-item>
</a-form></template><script>import { UserOutlined, LockOutlined } from "@ant-design/icons-vue";import { Form, Input, Button } from "ant-design-vue";import { reactive } from "vue";export default { components: {
UserOutlined,
LockOutlined,
[Form.name]: Form,
[Form.Item.name]: Form.Item,
[Input.name]: Input,
[Button.name]: Button
},
setup() { const state = reactive({ form: { user: "", password: ""
}
}); function handleSubmit() { console.log(state.form);
} return {
state,
handleSubmit
};
}
};</script>复制代码</pre> and then restart the project, you can find that it can be used normally
is gone.
, what attracts everyone’s attention most is Vue3 .0's Composition API, for Composition API, it can be said that the polarization is particularly serious. Some people particularly like this new design and development method, while others I feel that it is easy to write spaghetti-style code using Composition API (maybe these people don’t know Lanzhou Ramen). I will not comment on whether Composition API is good or bad. Anyway, I am just a developer. The setup introduced in this section is the entrance to the Composition API. <h4 data-id="heading-5">setup介绍</h4>
<p><code>setup是Vue3.0提供的一个新的属性,可以在setup中使用Composition API,在上面的示例代码中我们已经使用到了setup,在上文代码中我们在setup中通过reactive初始化了一个响应式数据,然后通过return返回了一个对象,对象中包含了声明的响应式数据和一个方法,而这些数据就可以直接使用到了template中了,就像上文代码中的那样。关于reactive,我将会在下一小节为你带来说明。
setup函数有两个参数,分别是props和context。
props
props是setup函数的第一个参数,是组件外部传入进来的属性,与vue2.0的props基本是一致的,比如下面代码
export default { props: { value: { type: String, default: ""
}
},
setup(props) { console.log(props.value)
}
}复制代码但是需要注意的是,在setup中,props是不能使用解构的,即不能将上面的代码改写成
setup({value}) { console.log(value)
}复制代码虽然template中使用的是setup返回的对象,但是对于props,我们不需要在setup中返回,而是直接可以在template使用,比如上面的value,可以直接在template写成
<custom-component :value="value"></custom-component>复制代码
context
context是setup函数的第二个参数,context是一个对象,里面包含了三个属性,分别是
attrs
attrs与Vue2.0的this.$attrs是一样的,即外部传入的未在props中定义的属性。对于attrs与props一样,我们不能对attrs使用es6的解构,必须使用attrs.name的写法
slots
slots对应的是组件的插槽,与Vue2.0的this.$slots是对应的,与props和attrs一样,slots也是不能解构的。
emit
emit对应的是Vue2.0的this.$emit, 即对外暴露事件。
setup函数一般会返回一个对象,这个对象里面包含了组件模板里面要使用到的data与一些函数或者事件,但是setup也可以返回一个函数,这个函数对应的就是Vue2.0的render函数,可以在这个函数里面使用JSX,对于Vue3.0中使用JSX,小编将在后面的系列文章中为您带来更多说明。
最后需要注意的是,不要在
setup中使用this,在setup中的this和你真正要用到的this是不同的,通过props和context基本是可以满足我们的开发需求的。
Composition API,先从reactive和ref开始在使用Vue2.0的时候,我们一般声明组件的属性都会像下面的代码一样
export default {
data() { return { name: '子君', sex: '男'
}
}
}复制代码然后就可以在需要用到的地方比如computed,watch,methods,template等地方使用,但是这样存在一个比较明显的问题,即我声明data的地方与使用data的地方在代码结构中可能相距很远,有一种君住长江头,我住长江尾,日日思君不见君,共饮一江水的感觉。而Composition API的诞生的一个很重要的原因就是解决这个问题。在尤大大在关于Composition API的动机中是这样描述解决的问题的:
现在我们先了解一下Compositon API中的reactive和ref
reactive
在Vue2.6中, 出现了一个新的api,Vue.observer,通过这个api可以创建一个响应式的对象,而reactive就和Vue.ovserver的功能基本是一致的。首先我们先来看一个例子
<template>
<!--在模板中通过state.name使用setup中返回的数据-->
<p>{{ state.name }}</p></template><script>import { reactive } from "vue";export default {
setup() { // 通过reactive声明一个可响应式的对象
const state = reactive({ name: "子君"
}); // 5秒后将子君修改为 前端有的玩
setTimeout(() => {
state.name = "前端有的玩";
}, 1000 * 5); // 将state添加到一个对象中然后返回
return {
state
};
}
};</script>复制代码上面的例子就是reactive的一个基本的用法,我们通过上面的代码可以看到reactive和Vue.observer声明可响应式对象的方法是很像的,但是他们之间还是存在一些差别的。我们在使用vue2.0的时候,最常见的一个问题就是经常会遇到一些数据明明修改了值,但是界面却并没有刷新,这时候就需要使用Vue.set来解决,这个问题是因为Vue2.0使用的Object.defineProperty无法监听到某些场景比如新增属性,但是到了Vue3.0中通过Proxy将这个问题解决了,所以我们可以直接在reactive声明的对象上面添加新的属性,一起看看下面的例子
<template> <p>
<p>姓名:{{ state.name }}</p>
<p>公众号:{{ state.gzh }}</p>
</p></template>
<script>
import { reactive } from "vue";
export default {
setup() {
const state = reactive({
name: "子君"
});
// 5秒后新增属性gzh 前端有的玩
setTimeout(() => {
state.gzh = "前端有的玩";
}, 1000 * 5);
return {
state
};
}
};
</script>复制代码上面的例子虽然在state中并没有声明gzh属性,但是在5s后我们可以直接给state添加gzh属性,这时候并不需要使用Vue.set来解决新增属性无法响应的问题。
在上面的代码中,reactive通过传入一个对象然后返回了一个state,需要注意的是state与传入的对象是不用的,reactive对原始的对象并没有进行修改,而是返回了一个全新的对象,返回的对象是Proxy的实例。需要注意的是在项目中尽量去使用reactive返回的响应式对象,而不是原始对象。
const obj = {}const state = reactive(obj)// 输出falseconsole.log(obj === state)复制代码ref
假如现在我们需要在一个函数里面声明用户的信息,那么我们可能会有两种不一样的写法
// 写法1let name = '子君'let gzh = '前端有的玩'// 写法2let userInfo = { name: '子君', gzh: '前端有的玩'}复制代码上面两种不同的声明方式,我们使用的时候也是不同的,对于写法1我们直接使用变量就可以了,而对于写法2,我们需要写成userInfo.name的方式。我们可以发现userInfo的写法与reactive是比较相似的,而Vue3.0也提供了另一种写法,就像写法1一样,即ref。先来看一个例子。
<template> <p>
<p>姓名:{{ name }}</p>
</p></template>
<script>
import { ref } from "vue";
export default {
setup() {
const name = ref("子君");
console.log('姓名',name.value)
// 5秒后修改name为 前端有的玩
setTimeout(() => {
name.value = "前端有的玩";
}, 1000 * 5);
return {
name
};
}
};
</script>复制代码通过上面的代码,可以对比出来reactive与ref的区别
reactive传入的是一个对象,返回的是一个响应式对象,而ref传入的是一个基本数据类型(其实引用类型也可以),返回的是传入值的响应式值reactive获取或修改属性可以直接通过state.prop来操作,而ref返回值需要通过name.value的方式来修改或者读取数据。但是需要注意的是,在template中并不需要通过.value来获取值,这是因为template中已经做了解套。Vue3.0优雅的使用v-model
v-model并不是vue3.0新推出的新特性,在Vue2.0中我们已经大量的到了v-model,但是V3和V2还是有很大的区别的。本节我们将主要为大家带来如何在Vue3.0中使用v-model,Vue3.0中的v-model提供了哪些惊喜以及如何在Vue3.0中自定义v-model。
Vue2.0和Vue3.0中使用v-model
在Vue2.0中如何实现双向数据绑定呢?常用的方式又两种,一种是v-model,另一种是.sync,为什么会有两种呢?这是因为一个组件只能用于一个v-model,但是有的组件需要有多个可以双向响应的数据,所以就出现了.sync。在Vue3.0中为了实现统一,实现了让一个组件可以拥有多个v-model,同时删除掉了.sync。如下面的代码,分别是Vue2.0与Vue3.0使用v-model的区别。
在Vue2.0中使用v-model
<template>
<a-input v-model="value" placeholder="Basic usage" /></template><script>export default {
data() { return { value: '',
};
},
};</script>复制代码在Vue3.0中使用v-model
<template>
<!--在vue3.0中,v-model后面需要跟一个modelValue,即要双向绑定的属性名-->
<a-input v-model:value="value" placeholder="Basic usage" /></template><script>export default { // 在Vue3.0中也可以继续使用`Vue2.0`的写法
data() { return { value: '',
};
},
};</script>复制代码在vue3.0中,v-model后面需要跟一个modelValue,即要双向绑定的属性名,Vue3.0就是通过给不同的v-model指定不同的modelValue来实现多个v-model。对于v-model的原理,下文将通过自定义v-model来说明。
v-model
Vue2.0自定义一个v-model示例<template>
<p class="custom-input">
<input :value="value" @input="$_handleChange" />
</p></template><script>export default { props: { value: { type: String, default: ''
}
}, methods: {
$_handleChange(e) { this.$emit('input', e.target.value)
}
}
}</script>复制代码在代码中使用组件
<template>
<custom-input v-model="value"></custom-input></template><script>
export default {
data() { return { value: ''
}
}
}</script>复制代码在Vue2.0中我们通过为组件设置名为value属性同时触发名为input的事件来实现的v-model,当然也可以通过model来修改属性名和事件名,可以看我以前的文章中有详解。
Vue3.0自定义一个v-model示例组件代码
<template>
<p class="custom-input">
<input :value="value" @input="_handleChangeValue" />
</p></template><script>export default { props: { value: { type: String, default: ""
}
}, name: "CustomInput",
setup(props, { emit }) { function _handleChangeValue(e) { // vue3.0 是通过emit事件名为 update:modelValue来更新v-model的
emit("update:value", e.target.value);
} return {
_handleChangeValue
};
}
};</script>复制代码在代码中使用组件
<template>
<!--在使用v-model需要指定modelValue-->
<custom-input v-model:value="state.inputValue"></custom-input></template><script>import { reactive } from "vue";import CustomInput from "../components/custom-input";export default { name: "Home", components: {
CustomInput
},
setup() { const state = reactive({ inputValue: ""
}); return {
state
};
}
};</script>复制代码到了Vue3.0中,因为一个组件支持多个v-model,所以v-model的实现方式有了新的改变。首先我们不需要使用固定的属性名和事件名了,在上例中因为是input输入框,属性名我们依然使用的是value,但是也可以是其他任何的比如name,data,val等等,而在值发生变化后对外暴露的事件名变成了update:value,即update:属性名。而在调用组件的地方也就使用了v-model:属性名来区分不同的v-model。
在本文中我们主要讲解了开发环境的搭建,setup,reactive,ref,v-model等的介绍,同时通过对比Vue3.0与Vue2.0的不同,让大家对Vue3.0有了一定的了解,在下文中我们将为大家带来更多的介绍,比如技术属性,watch,生命周期等等,敬请期待。本文首发于公众号【前端有的玩】,学习Vue,面试刷题,尽在【前端有的玩】,乘兴裸辞心甚爽,面试工作屡遭难。 幸得每日一题伴,点击关注莫偷懒。,下周一新文推送,不见不散。
不要吹灭你的灵感和你的想象力; 不要成为你的模型的奴隶。 ——文森特・梵高
想了解更多编程学习,敬请关注php培训栏目!
The above is the detailed content of Knowledge points gained using Vue3.0 (1). For more information, please follow other related articles on the PHP Chinese website!