npm缓存位置
https://blog.csdn.net/yeahPeng11/article/details/120606669#_33
Vue项目创建
npm create vue@latest
Vue项目启动代码执行流程分析
Vue项目创建好之后都会有这三个文件:index.html 、main.js 、App.vue;
index.html—主页,项目入口
App.vue—根组件
main.js—入口文件
1、index.html :类似于挂载点,所有的Vue页面全部将会挂载到此html下
2、main.js : 控制有关Vue功能,控制那些Vue页面挂载到html下
3.App.vue:页面样式
API风格
选项式
<script>
export default {
// data() 返回的属性将会成为响应式的状态
// 并且暴露在 `this` 上
data() {
return {
count: 0
}
},
// methods 是一些用来更改状态与触发更新的函数
// 它们可以在模板中作为事件处理器绑定
methods: {
increment() {
this.count++
}
},
// 生命周期钩子会在组件生命周期的各个不同阶段被调用
// 例如这个函数就会在组件挂载完成后被调用
mounted() {
console.log(`The initial count is ${this.count}.`)
}
}
</script>
<template>
<button @click="increment">Count is: {{ count }}</button>
</template>
组合式
<script setup>
import { ref, onMounted } from 'vue'
// 响应式状态
const count = ref(0)
// 用来修改状态、触发更新的函数
function increment() {
count.value++
}
// 生命周期钩子
onMounted(() => {
console.log(`The initial count is ${count.value}.`)
})
</script>
<template>
<button @click="increment">Count is: {{ count }}</button>
</template>
Attribute 绑定
双大括号不能在 HTML attributes 中使用
想要响应式地绑定一个 attribute,应该使用 v-bind 指令:
<div v-bind:id="dynamicId"></div>
ref()
1.在组合式 API 中,推荐使用 ref() 函数来声明响应式状态:
2.ref() 接收参数,并将其包裹在一个带有 .value 属性的 ref 对象中返回:
3.要在组件模板中访问 ref,请从组件的 setup() 函数中声明并返回它们
4.在模板中使用 ref 时,我们不需要附加 .value。为了方便起见,当在模板中使用时,ref 会自动解包 。
5.Ref 可以持有任何类型的值,包括深层嵌套的对象、数组或者 JavaScript 内置的数据结构
import { ref } from 'vue'
const count = ref(0)
console.log(count) // { value: 0 }
console.log(count.value) // 0
count.value++
console.log(count.value) // 1
<script setup>
import { ref } from 'vue'
const obj = ref({
nested: { count: 0 },
arr: ['foo', 'bar']
})
</script>
<template>
<div id="app">
<button @click="obj.nested.count++">Count is: {{ obj.nested.count }}</button>
</div>
</template>