首页 > 其他分享 >08-初始vue3

08-初始vue3

时间:2024-05-06 21:12:56浏览次数:22  
标签:Vue const 08 createApp reactive vue3 import app 初始

写在前面

https://juejin.cn/post/7106374777601785893

https://juejin.cn/post/7253828299938906169

配置项写法

<body>
    <div id="app">
    </div>
</body>
<script>
    const app = Vue.createApp({
        // ...
    })
    app.mount("#app")
</script>

配置项写法示例

<body>
    <div id="app">
        <button @click="count--">-</button>
        {{count}}
        <button @click="count++">+</button>
    </div>
</body>
<script>
    // 使用Vue.createApp创建了一个Vue应用程序实例。
    const app = Vue.createApp({
        data() {
            return {
                count: 3
            }
        },
    })
    // 使用app.mount("#app")将Vue应用程序实例挂载到id为"app"的div上,这样Vue应用程序就能够控制这部分HTML代码了。
    app.mount("#app")
</script>

组合式api写法

<body>
    <div id="app">
        <button @click="count--">-</button>
        {{count}}
        <button @click="count++">+</button>
    </div>
</body>
<script>
    const app = Vue.createApp({
        // setup函数用于设置组件的响应式数据,并返回必要的数据和方法。
        setup(props) {
            // let count = 3 无法双向绑定,如果要让值设置成响应式,需要使用Vue.ref(属性)
            let count = Vue.ref(3)
            return {
                count
            }
        }
    })
    // 使用app.mount("#app")将Vue应用程序实例挂载到id为"app"的div上,这样Vue应用程序就能够控制这部分HTML代码了。
    app.mount("#app")
</script>

通过点击事件触发函数操作

<body>
    <div id="app">
        <button @click="subCount">-</button>
        {{count}}
        <button @click="addCount">+</button>
    </div>
</body>
<script>
    const app = Vue.createApp({
        setup(props) {
            let count = Vue.ref(3);
            
            // 定义函数,函数名称就是触发点击事件后执行的函数
            function subCount() {
                count.value--
            }
            
            function addCount() {
                count.value++
            }
            
            // 别忘记返回
            return {
                count,
                subCount,
                addCount
            }
        }
    })
    // 别忘记挂载
    app.mount("#app")
</script>

将容器类型做成响应式

语法:const 代理对象= reactive(源对象)接收一个对象(或数组),返回一个代理对象(Proxy的实例对象,简称proxy对象)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vue3</title>
    <script src="../js/vue3.js"></script>
</head>
<body>
    <div id="app">
        <button @click="subAge">年龄-1</button>
        <p>
            <span>姓名:{{hero.name}}</span>
            <span>年龄:{{hero.age}}</span>
            <span>爱好:{{hero.hobby.join('、')}}</span>
        </p>
        <button @click="addAge">年龄+1</button>
    </div>
</body>
<script>
    const {createApp, reactive} = Vue
    const app = createApp({
        setup(props) {

            const baseData = {name: '小满', age:3, hobby: ['逃课', '摸鱼']}
            // const hero = Vue.reactive(baseData)
            const hero = reactive(baseData)
            function subAge() {
                hero.age--
            }

            function addAge() {
                hero.age++
            }

            return {
                hero,
                subAge,
                addAge
            }
        }
    })
    app.mount("#app")
</script>
</html>

简单总结

setup
# 1 如果使用配置项API---》写起来跟vue2一样
# 2 如果写组合式API---》所有代码都要写在setup函数中
	-后期除了setup函数,其他都不带了
    
# 3 setup函数必须返回变量或函数--》在template中才能使用
# 4 默认不带响应式,需要使用ref或reactive包裹


ref和reactive
# 1 ref 包裹值类型[数字,字符串,布尔],做成响应式
# 2 reactive包裹引用类型[对象,数组],做成响应式
# 3 使用reactive包裹的对象,直接通过 .  [] 取值赋值就是响应式的
	ref包裹的对象,需要使用 对象.value 修改值
# 4 使用ref包裹的,在template中,不许用使用 变量.value

import {} from 'Vue' 和 const {} = Vue 的区别

import {} from 'Vue'const {} = Vue 是两种不同的导入 Vue 的方式,它们的区别在于语法和用途。

  1. import {} from 'Vue':

    • 这是 ES6 的模块导入语法,用于导入模块中的特定成员。
    • 当你使用 import {} from 'Vue' 时,你可以选择性地导入 Vue 模块中的特定成员,例如 createAppreactive 等。
    • 例如:import { createApp, reactive } from 'Vue'
  2. const {} = Vue:

    • 这是从一个对象中解构出特定成员的 JavaScript 语法。
    • 当你使用 const {} = Vue 时,你是从 Vue 对象中解构出特定的成员,使得你可以直接使用这些成员,而不需要每次都写 Vue.xxx
    • 例如:const { createApp, reactive } = Vue

综上所述,两种方式都可以用来导入 Vue 模块中的特定成员,但是语法略有不同。 import {} from 'Vue' 是 ES6 的模块导入语法,而 const {} = Vue 是从对象中解构出特定成员的 JavaScript 语法。

使用vite创建项目

https://cn.vitejs.dev/

执行命令

npm create vite@latest //创建项目
npm create vite@latest name // 创建项目的时候可以带名称

vue3使用router-view

  1. 在src下面新建一个文件夹router,然后里面新建一个index.js文件
  2. 在index.js中,写入逻辑。
import { createRouter, createWebHistory } from 'vue-router'

// 注意 这里一定要叫 routes 不然报错 Cannot read property ‘forEach‘ of undefined
const routes = [
    {
        path: "/book",
        name: "book",
        component: () => import("../views/BookView.vue")
    },
    {
        path: "/",
        name: 'home',
        component: () => import("../views/HomeView.vue")
    }
]

const router = createRouter({
    // history: createWebHashHistory, hash模式
    history: createWebHistory(), // history模式
    routes
})

export default router
  1. 在main.js中
import { createApp } from 'vue'
import router from "./router/index.js"
import './style.css'
import App from './App.vue'

const app = createApp(App)
app.use(router);
app.mount("#app")
  1. 在App.vue中
<template>
  <div>
    <router-view></router-view>
  </div>
</template>

完成。

标签:Vue,const,08,createApp,reactive,vue3,import,app,初始
From: https://www.cnblogs.com/ccsvip/p/18175950

相关文章

  • vue3
    【介绍】#vue3的变化1.性能的提升-打包大小减少41%-初次渲染快55%,更新渲染快133%-内存减少54%2.源码的升级使用Proxy代替defineProperty实现响应式重写虚拟DOM的实现和Tree-Shaking3.拥抱TypeScriptV......
  • 10.3顺序表的初始化以及插入实战(早期学习笔记记录)
    命名规范1.下划线命名法list_insert不同的单词用下划线连接2.驼峰命名法ListInsert每个单词首字母大写一切数据结构考的都是增(插入)删查改插入思路1、判断插入位置是否合法1<=i<=lenthif(i<1||i>lenth){returnfalse;}2、判断储存空间是否已满(插入数据后......
  • vue3 个人自适应配置方案
    ``使用插件"postcss-pxtorem":"^6.1.0",postcss.config.cjs文件配置module.exports={plugins:{autoprefixer:{overrideBrowserslist:["Android4.1","iOS7.1","Chrome>31......
  • *uniapp-vue3-ts项目配置eslint+prettier+husky
    代码检查工具:Eslint代码格式化工具:prettierhusky:Git客户端增加了钩子(hooks)功能,使得在特定阶段执行一系列流程,以保证每一个commit的正确性vscode安装插件:    安装eslint+prettier:npmi-Deslintprettiereslint-plugin-vue@vue/eslint-config-prettier@vu......
  • 基于arduino+as608+360舵机的宿舍指纹锁
    需要的制作材料Arduinonano开发板AS608指纹模块MG995360舵机(360舵机的控制与180舵机控制不相同,360舵机只可以控制旋转的速度,180舵机可以控制旋转的角度)面包板,电线若干准备部分参考了大佬的代码http://t.csdnimg.cn/GB29Qarduino模块可以看b站视频,,零基础(c语言零基础......
  • 208. 实现 Trie (前缀树)-python
    Trie(发音类似"try")或者说前缀树是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。请你实现Trie类:Trie()初始化前缀树对象。voidinsert(Stringword)向前缀树中插入字符串word。booleansearch(St......
  • APS54087 是 一款外围电路简单的无频闪降压LED恒流驱动器
     APS54087是一款外围电路简单的无频闪降压LED恒流驱动器,芯片采用高压工艺,减少整个电路的发热量,提高效率。适用于6.5-80V电压范围的非隔离式大功率恒流LED驱动领域。LD端口支持模拟调光,调光范围0-1.8V,应用于大电流设置。LD端口接电容到地,可以设置软起动时间。 APS54087......
  • Vue3 除了 keep-alive,还有哪些页面缓存的实现方案
    引言有这么一个需求:列表页进入详情页后,切换回列表页,需要对列表页进行缓存,如果从首页进入列表页,就要重新加载列表页。对于这个需求,我的第一个想法就是使用keep-alive来缓存列表页,列表和详情页切换时,列表页会被缓存;从首页进入列表页时,就重置列表页数据并重新获取新数据来达到列表......
  • 十分钟,带你了解 Vue3 的新写法
    本文的目的,是为了让已经有Vue2开发经验的 人 ,快速掌握Vue3的写法。因此, 本篇假定你已经掌握Vue的核心内容 ,只为你介绍编写Vue3代码,需要了解的内容。一、Vue3里 script 的三种写法首先,Vue3新增了一个叫做组合式api的东西,英文名叫CompositionAPI。因此Vue3......
  • vue3早已具备抛弃虚拟DOM的能力了
    前言jquery时代更新视图是直接对DOM进行操作,缺点是频繁操作真实DOM,性能差。react和vue时代引入了虚拟DOM,更新视图是对新旧虚拟DOM树进行一层层的遍历比较,然后找出需要更新的DOM节点进行更新。这样做的缺点就是如果DOM树很复杂,在进行新旧DOM树比较的时候性能就比较差了。那么有没......