举个例子:

<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
Vue.createApp({
setup() {
const msg = 'Hello World'
return { msg }
}
}).mount('#app')
</script>判断是否为传统写法的依据:
new Vue()new Vue({...}) 是 Vue 2 的标准写法,也是 "全局 Vue 对象 + 根实例" 的模式。new Vue(),而是用 createApp(App) 来创建应用实例。<script src="https://cdn.jsdelivr.net/.../vue.js"></script> 然后写 new Vue({...}),通常就是快速 demo / 教学 / 小型页面,不使用模块化打包工具。data + methods + Vue.set 这种选项式 API,也属于传统开发模式。<script setup>,不需要 Vue.set,逻辑可拆分为可复用的 composable 函数。import { createApp, ref } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')在构建工具(Vite/Webpack)环境下开发 Vue,这是最推荐的、也是企业采用的方式。其中 App.vue 是一个单文件组件(SFC),会被构建工具编译成 JS 模块。

特点 | 学习版(CDN) | 模块化(ES Module) |
|---|---|---|
适用场景 | 快速演示、小项目 | 现代大型项目 |
引入方式 | <script src="vue.js"> | import { createApp } from 'vue' |
组件写法 | 直接写在 createApp({ setup(){...} }) | 单文件组件 .vue,逻辑写在 setup() |
推荐度 | 初学者 | 实战项目 |
官网:https://nodejs.org/en/
node -v
npm -vnpm换源:
// 查看 npm 源
npm config get registry
// 默认是指向 https://registry.npmjs.org/,也就是官方源
// 为了提高npm下载速度, 可以给npm换源
// 国内源有很多,我这里用淘宝源吧。毕竟是大公司,会比较稳定
npm config set registry https://registry.npmmirror.com
// 再一次查看 npm 源
npm config get registryyarn 和 pnpm、还有 npm三者的功能类似,都是包管理工具,用来下载或删除模块包,性能上 yarn 和 pnpm 优于 npm。
# windows系统
npm install yarn -g
npm install pnpm -g
___________________________________
# mac系统
sudo npm install yarn -g
sudo npm install pnpm -g检测是否安装成功:
yarn -v
pnpm -vnpm create vue@latest,会安装并执行 create-vue,它是 Vue 官方的项目脚手架工具cd 项目名称npm inpm run dev,会开启一个本地服务器

main.js:整个项目打包的入口,用于创建应用,是 Vue 代码通向网页代码的桥梁App.vue:Vue 代码的入口(根组件)index.html:项目入口网页.vue 的文件来开发项目script(JS) + template(HTML) + style(CSS)scoped 属性,保证样式只针对当前 template 内的标签生效vite 打包成 js、css 等,最终交给 index.html 通过浏览器呈现效果<script>
export default {
setup() {
// ...
const msg = 'Hello Vue3+Vite'
return {
msg
}
}
}
</script>
<template>
<h1>{{ msg }}</h1>
</template>
<style></style><script setup>
const msg = 'Hello Vue3+Vite'
</script>
<template>
<h1>{{ msg }}</h1>
</template>
<style></style><script setup>
// 导入响应式函数
import { reactive, ref } from 'vue'
// 字符串
const msg = ref('Hello Vue3+Vite')
// 对象
const obj = reactive({
name: '小vue',
age: 9
})
// 函数
function fn() {
return 100
}
</script>
<template>
<!-- 1. 直接放变量 -->
<h1>{{ msg }}</h1>
<!-- 2. 对象.属性 -->
<p>我叫 {{ obj.name }}, 今年 {{ obj.age }} 岁</p>
<!-- 3. 三元表达式 -->
<p>{{ obj.age >= 18 ? '已成年' : '未成年' }}</p>
<!-- 3. 算数运算 -->
<p>来年我就 {{ obj.age + 1 }} 岁了</p>
<!-- 4. 函数的调用 -->
<p>fn的返回值是: {{ fn() }}</p>
<!-- 4. 方法的调用 -->
<p>{{ msg }} 中含有 {{ msg.split(' ').length }} 个单词</p>
</template>指令是 Vue 提供的带有 v- 前缀的特殊标签属性,用来增强标签、提高标签数据渲染的能力。vue3 中的指令按照不同的用途可以分为如下六大类:
v-html && v-text作用:辅助开发者渲染 DOM 元素的文本内容。
v-text(类似 innerText)v-html(类似 innerHTML)代码示例:
<script setup>
import { ref } from 'vue'
const str = ref('<span style="color:red;">Hello Vue3+Vite</span>')
</script>
<template>
<div>
<p v-text="str"></p>
<p v-html="str"></p>
</div>
</template>
<style scoped></style>
v-bind作用:把表达式的结果与标签的属性动态绑定。
语法如下所示:
v-bind:属性名="表达式":属性名="表达式"代码实例:
<script setup>
import { ref } from 'vue'
const url = ref('http://www.baidu.com')
</script>
<template>
<div>
<!-- v-bind: 标签属性动态绑定 -->
<a v-bind:href="url">百度一下</a>
<!-- 简写 -->
<a :href="url">百度一下</a>
</div>
</template>v-on作用:给元素绑定事件。
有三种不同语法,以给按钮添加点击事件为例:
<button v-on:事件名="内联语句">按钮</button>
<button v-on:事件名="处理函数">按钮</button>
<button v-on:事件名="处理函数(实参列表)">按钮</button>v-on:可以简写为 @<script> 下声明
代码示例:
<script setup>
import { ref } from 'vue'
// 响应式数据 计数器
const count = ref(0)
// 无参函数
const increase = () => {
count.value++
}
// 有参函数
const add = (n) => {
count.value +=n
}
</script>
<template>
<div>
<p>{{ count }}</p>
<!-- 1. 内联语句 -->
<button v-on:click="count++">+1</button>
<!-- 2. 调用无参函数 -->
<button v-on:click="increase">+1</button>
<!-- 3. 调用有参函数 -->
<button v-on:click="add(3)">+3</button>
<button v-on:click="add(5)">+5</button>
<!-- 4. 简写 @click -->
<button @click="add(5)">+5</button>
</div>
</template>v-show && v-if作用:辅助开发者控制 DOM 的显示与隐藏。
v-showv-show="布尔表达式",表达式值为 true 显示,false 隐藏display:none 控制显示隐藏v-ifv-if="布尔表达式",表达式值 true 显示,false 隐藏v-else 和 v-else-ifv-else v-else-if="表达式"代码示例:
<script setup>
import { ref } from 'vue'
// 是否可见
const visible = ref(true)
// 是否登录
const isLogin = ref(true)
// 成绩
const mark = ref(100)
</script>
<template>
<!-- v-show -->
<div class="red" v-show="visible"></div>
<!-- v-if -->
<div class="green" v-if="visible"></div>
<hr>
<!-- 双分支的条件渲染 -->
<div v-if="isLogin">xxx, 欢迎回来</div>
<div v-else>你好, 请登录</div>
<hr>
<!--
多分支的条件渲染:
1. 90及其以上优秀
2. 70到90之间良好
3. 其他的差
-->
<div v-if="mark >= 90">优秀</div>
<div v-else-if="mark >= 70">良好</div>
<div v-else>差</div>
</template>
<style scoped>
.red, .green {
width: 200px;
height: 200px;
}
.red {
background: red;
}
.green {
background: green;
}
</style>v-for作用:基于数组进行遍历列表渲染
v-for 指令需要使用 (item, index) in 目标结构 形式的特殊语法,其中:
item:数组中的每一项index:每一项的索引,不需要可以省略<script setup>
import { ref } from 'vue'
// 数字数组
const nums = ref([11, 22, 33, 44])
// 商品列表
const goodsList = ref([
{ id: 1, name: '篮球', price: 299 },
{ id: 2, name: '足球', price: 99 },
{ id: 3, name: '排球', price: 199 }
])
// 准备对象
const obj = {
id: 10001,
name: 'liren',
age: 9
}
</script>
<template>
<div>
<ul>
<!-- 遍历数字数组 -->
<li v-for="(item, index) in nums">{{ item }} => {{ index }}</li>
</ul>
<div class="goods-list">
<!-- 遍历对象数组 -->
<div
class="goods-item"
v-for="item in goodsList">
<p>id = {{ item.id }}</p>
<p>name = {{ item.name }}</p>
<p>price = {{ item.price }}</p>
</div>
<ul>
<!-- 遍历对象 -->
<li v-for="(value, key, index) in obj">
{{ value }} => {{ key }} => {{ index }}
</li>
</ul>
<ul>
<!-- 遍历数字 -->
<li v-for="(item, index) in 5">{{ item }} => {{ index }}</li>
</ul>
</div>
</div>
</template>
<style scoped></style>v-for)index,利用 splice 删除)
<!-- @format -->
<script setup>
import { ref } from 'vue'
// 图书列表
const bookList = ref([
{ id: 1, name: '《红楼梦》', author: '曹雪芹' },
{ id: 2, name: '《西游记》', author: '吴承恩' },
{ id: 3, name: '《三国演义》', author: '罗贯中' },
{ id: 4, name: '《水浒传》', author: '施耐庵' }
])
// 删除函数
const onDel = (i) => {
// i: 当前点击的下标
if (window.confirm('确定删除么?')) {
// 调用 splice 进行删除
bookList.value.splice(i, 1)
}
}
</script>
<template>
<h3>书架</h3>
<ul>
<li
v-for="(item, index) in bookList"
:key="item.id"
>
<span>{{ item.name }}</span>
<span>{{ item.author }}</span>
<button @click="onDel(index)">删除</button>
</li>
</ul>
</template>
<style>
#app {
width: 400px;
margin: 100px auto;
}
ul {
list-style: none;
}
ul li {
display: flex;
justify-content: space-around;
padding: 10px 0;
border-bottom: 1px solid #ccc;
}
</style>v-for 添加 key语法::key="唯一值"
作用:给列表项添加的唯一标识,便于 Vue 进行列表项的正确排序复用,因为 Vue 的默认行为会尝试原地修改元素(就地复用)
注意事项:
key 的类型只能是数字或字符串key 的值必须唯一, 不能重复id 作为 key(因为id唯一),不推荐用 index 作为 key(会变化)
v-model所谓双向绑定就是:
作用在表单元素(input、select、radio、checkbox)上,实现数据双向绑定,从而可以快速获取或设置表单元素的值。
语法:v-model="响应式数据"
使用双向绑定实现以下需求:

<script setup>
import { reactive } from 'vue'
// 登录表单对象
const loginForm = reactive({
username: '',
password: ''
})
</script>
<template>
<div class="login-box">
账户:<input type="text" v-model="loginForm.username"/><br /><br />
密码:<input type="password" v-model="loginForm.password"/><br /><br />
<button>登 录</button>
<button>重 置</button>
</div>
</template>原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。