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传递事件和参数

相关推荐
samuel91810 小时前
pinia实现数据持久化插件pinia-plugin-persist-uni
前端·vue
pixle01 天前
Vue3 Echarts 3D圆形柱状图实现教程以及封装一个可复用的组件
前端·3d·vue·echarts
机构师1 天前
<uniapp><插件><UTS>在uniapp中,创建自己的插件并发布到uni插件市场
javascript·uni-app·vue·插件·hbuilderx·uni
学了就忘2 天前
Axios 传参与 Spring Boot 接收参数完全指南
java·spring boot·后端·vue
局外人LZ3 天前
WXT+Vue3+sass+antd+vite搭建项目开发chrome插件
前端·chrome·vue·sass
Passerby_K4 天前
vue3+dhtmlx 甘特图真是案例
前端·vue·甘特图
码农研究僧4 天前
Vue3 上传后的文件智能预览(实战体会)
vue·html·图片预览
Bald Monkey4 天前
【Element Plus】解决移动设备使用 el-menu 和 el-sub-menu 时,子菜单需要点击两次才会隐藏的问题
前端·elementui·vue·element plus
若愚67924 天前
Vue3 + OpenLayers 开发教程 (五)移动端适配与数据可视化
前端·arcgis·vue
若愚67924 天前
Vue3 + OpenLayers 开发教程 (四) 样式配置与性能优化
前端·arcgis·vue