vue3入门

文章目录

安装VUE3

安装nodejs

官网下载:

https://nodejs.cn/download/

里面包含了npm包,安装完成后也自动安装了npm

node -v

vue3要求Node的版本至少16版本上

安装淘宝镜像

用管理员启动

npm install -g cnpm --registry=https://registry.npm.taobao.org

验证安装成功

bash 复制代码
cnpm config get registry

vue 安装

bash 复制代码
cnpm install vue -g
npm install -g @vue/cli

验证安装成功

bash 复制代码
vue -v

webpack安装

bash 复制代码
cnpm i webpack webpack-cli webpack-dev-server -g

vue3不依赖webpack可以不装

安装eslint

bash 复制代码
vue add @vue/cli-plugin-eslint

vite创建一个项目

官方的构建工具Vite和项目脚手架create-vue. 通过以下命令生成一个项目:

要注意在✔ Add TypeScript?时要选择Yes才能使用TypeScript. 其他选项选No即可

bash 复制代码
cnpm init vue@latest

然后根据最后的提示:

bash 复制代码
Done. Now run:

  cd vue-project
  npm install
  npm run dev

一步步执行:

bash 复制代码
VITE v4.4.11  ready in 619 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help

打开 http://localhost:5173/即可

可以直接vue创建一个项目

bash 复制代码
vue create vue-project

执行过程输出:

bash 复制代码
cd vue-project
npm run serve

按照步骤执行,输出

bash 复制代码
App running at:
  - Local:   http://localhost:8080/
  - Network: http://172.22.178.164:8080/

打开浏览器即可

vue3使用

工程目录介绍

  • src目录:

main.js 入口文件,createApp函数创建应用实例

App.vue 根组件

  • public目录:

index.html,提供id为app的挂载点

核心就是组件怎么写,下面介绍

组件文件介绍

组件分为三个部分

  1. script 脚本,封装数据和函数
  2. template 模板,定义布局,显示script中获取的数据
  3. styple 页面样式
    下面是一个简单的示例:
bash 复制代码
<script>
export default{
  setup(){
    // 数据
    const message = 'message is here'

    //函数
    const logMessage = ()=> {
      console.log(message)
    }

    return {
      message,
      logMessage
    }
  }
  
}
</script>

<template>
  <div>
    {{ message }}
    <button @click="logMessage">log</button>
  </div>
</template>

<style>
</style>

为了简化写法,script中要返回所有数据,vue提供了语法糖script setup,所以简化写法就是:

bash 复制代码
<script setup>
// 数据
const message = 'message is here'

//函数
const logMessage = ()=> {
  console.log(message)
}
</script>

<template>
  <div>
    {{ message }}
    <button @click="logMessage">log</button>
  </div>
</template>

<style>
</style>

基础语法

响应式对象

页面需要交互式的组件,一个静态的按钮没有任何作用,这是就需要用到响应式对象

reactive 只能用于对象类型

ref 可用于简单类型和对象烈性

一个简单的示例:

bash 复制代码
<script setup>
  import { reactive } from 'vue';
  const value = reactive({count:0})
  const plusvalue =()=>{
    value.count++
  }

  import { ref } from 'vue';
  const va = ref(0)
  const plusva =()=>{
    va.value++
  }
</script>

<template>
  <div>
    <!-- {{ message }} -->
    <button @click="plusvalue">{{ value.count }}</button>
    <button @click="plusva">{{ va }}</button>
    
  </div>
</template>

<style>
</style>

如果不用ref和reactive,点击button的时候页面不会有任何反应,无法交互

watch监听

监听响应对象的变化,选项 immediate

immediate 组件加载加载时就先执行一次watch

bash 复制代码
watch(va, ()=>{
    console.log("va change")
  },
  {
    immediate:true
  }
  )

可以只侦听test中某一个值,而不是所有值

bash 复制代码
<script setup>
  import { reactive,  watch} from 'vue';
  const test = reactive({count:0, name:'abc'})
  const pluscount =()=>{
    test.count++
  }
  
  const changename =()=>{
    test.name = 'def'
  }

  watch(()=>test.count,  
  ()=>{
    console.log("test count change")
  })
</script>

<template>
  <div>
    <!-- {{ message }} -->
    <button @click="pluscount">{{ test.count }} count</button>
    <button @click="changename">{{ test.name }} name</button>
  </div>
</template>

此处只有点击 count的按钮的时候,watch中的日志才能打印

父子组件通信

父传子

父组件引入子组件,且绑定子组件属性

子组件使用

父组件:

html 复制代码
<script setup>
  // 引入子组件
  import HelloWorld from './components/HelloWorld.vue';

</script>

<template>
  <div>
    <!-- 绑定属性 message -->
    <HelloWorld message="hello world message"/>
  </div>
</template>
<style>
</style>

子组件:

html 复制代码
<script setup>
  import { defineProps } from 'vue'
  // 子组件接收属性
  const props = defineProps({
    message: String
  })

  console.log(props)
</script>

<template>
  <div>
    这是父组件传递的信息{{ message }}
  </div>
</template>
子传父

父组件给子组件绑定事件

子组件通过emit传递事件和参数

相关推荐
麦麦大数据7 小时前
F003疫情传染病数据可视化vue+flask+mysql
mysql·flask·vue·大屏·传染病
知识分享小能手2 天前
Vue3 学习教程,从入门到精通,Axios 在 Vue 3 中的使用指南(37)
前端·javascript·vue.js·学习·typescript·vue·vue3
码码哈哈爱分享2 天前
Tauri 框架介绍
css·rust·vue·html
i紸定i3 天前
解决html-to-image在 ios 上dom里面的图片不显示出来
前端·ios·vue·html·html-to-image
尚学教辅学习资料4 天前
Vue3从入门到精通: 4.5 数据持久化与同步策略深度解析
vue·数据持久化
IT毕设实战小研5 天前
Java毕业设计选题推荐 |基于SpringBoot的健身爱好线上互动与打卡社交平台系统 互动打卡小程序系统
java·开发语言·vue.js·spring boot·vue·毕业设计·课程设计
第七种黄昏5 天前
大事件项目拆解:登录访问拦截实现详解
前端框架·vue·js
har01d6 天前
在 uniapp 里使用 unocss,vue3 + vite 项目
前端·uni-app·vue·uniapp·unocss
har01d8 天前
【CSS3】录音中。。。
前端·css·vue.js·vue·vue3·css3
柯北(jvxiao)8 天前
Vue vs React 多维度剖析: 哪一个更适合大型项目?
前端·vue·react