通过10个实例小练习,快速入门熟练 Vue3.0 核心新特性(上)

简介: 通过10个实例小练习,快速入门熟练 Vue3.0 核心新特性

环境搭建


$ git pull https://github.com/vuejs/vue-next.git
$ cd vue-next && yarn


下载完成之后打开代码, 开启 sourceMap :


  • tsconfig.json 把 sourceMap 字段修改为 true: "sourceMap": true
  • rollup.config.js 在 rollup.config.js 中,手动键入: output.sourcemap = true
  • 生成 vue 全局的文件:yarn dev
  • 在根目录创建一个 demo 目录用于存放示例代码,并在 demo 目录下创建 html 文件,引入构建后的 vue 文件



api 的使用都是很简单的,下文的内容,看例子代码就能懂了的,所以下面的例子不会做过多解释。


reactive

reactive: 创建响应式数据对象setup 函数是个新的入口函数,相当于 vue2.x 中 beforeCreate 和 created,在 beforeCreate 之后 created 之前执行。
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>reactive</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive } = Vue
    const App = {
        template: `
            <button @click='click'>reverse</button> 
            <div style="margin-top: 20px">{{ state.message }}</div>
        `,
        setup() {
            console.log('setup ');
            const state = reactive({
                message: 'Hello Vue3!!'
            })
            click = () => {
                state.message = state.message.split('').reverse().join('')
            }
            return {
                state,
                click
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183419.gif


ref & isRef

ref : 创建一个响应式的数据对象isRef : 检查值是否是 ref 的引用对象。

isRef : 检查值是否是 ref 的引用对象。


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>ref & isRef</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef } = Vue
    const App = {
        template: `
            <button @click='click'>count++</button> 
            <div style="margin-top: 20px">{{ count }}</div>
        `,
        setup() {
            const count = ref(0);
            console.log("count.value:", count.value)  // 0
            count.value++
            console.log("count.value:", count.value)  // 1
            // 判断某值是否是响应式类型
            console.log('count is ref:', isRef(count))
            click = () => {
                count.value++;
                console.log("click count.value:", count.value) 
            }
            return {
                count,
                click,
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183549.gif


Template Refs


使用 Composition API 时,反应性引用和模板引用的概念是统一的。


为了获得对模板中元素或组件实例的引用,我们可以像往常一样声明 ref 并从 setup() 返回。


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>Template Refs</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef, toRefs, onMounted, onBeforeUpdate } = Vue
    const App = {
        template: `
            <div v-for="(item, i) in list" :ref="el => { divs[i] = el }">
                {{ item }}
            </div>
        `,
        setup() {
            const list = reactive([1, 2, 3])
            const divs = ref([])
            // make sure to reset the refs before each update
            onBeforeUpdate(() => {
                divs.value = []
            })
            onMounted(() => {
                // the DOM element will be assigned to the ref after initial render
                console.log(divs.value) // [<div/>]
            })
            return {
                list,
                divs
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183839.gif



<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>Template Refs</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef, toRefs, onMounted, onBeforeUpdate } = Vue
    const App = {
        template: `
            <div v-for="(item, i) in list" :ref="el => { divs[i] = el }">
                {{ item }}
            </div>
        `,
        setup() {
            const list = reactive([1, 2, 3])
            const divs = ref([])
            // make sure to reset the refs before each update
            onBeforeUpdate(() => {
                divs.value = []
            })
            onMounted(() => {
                // the DOM element will be assigned to the ref after initial render
                console.log(divs.value) // [<div/>]
            })
            return {
                list,
                divs
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183910.png


toRefs


toRefs : 将响应式数据对象转换为单一响应式对象

将一个 reactive 代理对象打平,转换为 ref 代理对象,使得对象的属性可以直接在 template 上使用。


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>toRefs</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef, toRefs } = Vue
    const App = {
        // template: `
        //     <button @click='click'>reverse</button> 
        //     <div style="margin-top: 20px">{{ state.message }}</div>
        // `,
        // setup() {
        //     const state = reactive({
        //         message: 'Hello Vue3.0!!'
        //     })
        //     click = () => {
        //         state.message = state.message.split('').reverse().join('')
        //         console.log('state.message: ', state.message)
        //     }
        //     return {
        //         state,
        //         click
        //     }
        // }
        template: `
            <button @click='click'>count++</button> 
            <div style="margin-top: 20px">{{ message }}</div>
        `,
        setup() {
            const state = reactive({
                message: 'Hello Vue3.0!!'
            })
            click = () => {
                state.message = state.message.split('').reverse().join('')
                console.log('state.message: ', state.message)
            }
            return {
                click,
                ...toRefs(state)
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183956.gif

相关文章
|
9月前
|
JavaScript 前端开发 开发者
Vue第1天:特性概览
Vue第1天:特性概览
|
9月前
|
缓存 JavaScript
Vue3 的模板语法:指令、插值语法和其他相关特性
Vue3 的模板语法:指令、插值语法和其他相关特性
223 4
Vue3 的模板语法:指令、插值语法和其他相关特性
|
9月前
|
JavaScript 算法 编译器
Vue3的新特性
Vue3的新特性
212 0
|
6月前
|
JavaScript 前端开发 API
实用!最新的几个 Vue 3 重要特性提案
实用!最新的几个 Vue 3 重要特性提案
|
5月前
|
缓存 JavaScript 算法
卷不动也得继续学!紧跟vue3的步伐,再来get一波进阶新特性!
该文章深入讲解了Vue3的进阶新特性,包括`watchEffect`的使用、性能优化策略、Vite构建工具的优势以及全局API的变化等内容,帮助开发者更好地掌握Vue3的开发技巧。
卷不动也得继续学!紧跟vue3的步伐,再来get一波进阶新特性!
|
5月前
|
缓存 移动开发 JavaScript
查漏补缺方为上策!!两万六字总结vue的基本使用和高级特性,周边插件vuex和vue-router任你挑选
该文章全面总结了Vue.js的基本使用方法与高级特性,并介绍了Vue周边的重要插件Vuex和Vue-Router的使用技巧。
查漏补缺方为上策!!两万六字总结vue的基本使用和高级特性,周边插件vuex和vue-router任你挑选
|
4月前
|
缓存 JavaScript 算法
Vue3新特性合集
Vue3新特性合集
70 0
|
6月前
|
Rust JavaScript 前端开发
Rust! 无VDom! 尤雨溪解析 Vue.js 2024 新特性
Rust! 无VDom! 尤雨溪解析 Vue.js 2024 新特性
|
5月前
|
JavaScript API
卷死了!再不学vue3就没有人要你了!速来围观vue3新特性
该文章强调了学习Vue3的重要性,并详细介绍了Vue3相较于Vue2的新特性与改进,包括Composition API、响应式系统的变化以及其他API的更新等内容。
|
6月前
|
JavaScript 算法 API
Vue 3有哪些新特性
【8月更文挑战第16天】Vue 3有哪些新特性
568 1

热门文章

最新文章