网站怎么做收录,seo外包方法,免费软件大全,flash制作网站topVue tutorial 参考#xff1a;教程 | Vue.js (vuejs.org) 该教程需要前置知识#xff1a;HTML, CSS, JavaScript 学习前置知识#xff0c;你可以去 MDN Vue framework 是一个 JavaScript framework#xff0c;以下简称 Vue#xff0c;下面是它的特点
声明式渲染#xff…Vue tutorial 参考教程 | Vue.js (vuejs.org) 该教程需要前置知识HTML, CSS, JavaScript 学习前置知识你可以去 MDN Vue framework 是一个 JavaScript framework以下简称 Vue下面是它的特点
声明式渲染Declarative Rendering即声明 JavaScript 对象改变对象状态来更改 HTML这个过程由 Vue 完成响应式ReactivityJavaScript 的对象状态改变会马上反映到 DOM不知道 DOM 的去查 MDN 文档
Declarative Rendering and Reactivity
Vue 实现了
JavaScript Obejct - Vue - DOM但是 Vue 显然是利用 JavaScript 机制那就是 ProxyProxy 可以实现
JavaScript Object - Proxy - DOM所以 Vue 把 Proxy 改造后封装成了 reactive()调用这个 API 会返回一个特殊的对象称之为响应式对象reactive object。
reactive()
import { reactive } from vueconst counter reactive({count: 0
})console.log(counter.count) // 0
counter.count但是 reactive() 参数只能是对象还有数组和内置类型Vue 又把 reactive() 改造封装成了 ref()它也会返回一个响应式对象并且带一个 .value property只不过它的参数可以填写值。
ref()
import { ref } from vueconst message ref(Hello World!)console.log(message.value) // Hello World!
message.value Changed以上给 ref() 或 reactive() 填写参数得到响应式对象的过程就被成为数据绑定data binding。
Template syntax 在这里复习一下 HTML element 和 attribute 概念 Anatomy of an HTML element 这是 HTML element 这是 HTML attribute 在此之中Class 为 attribute nameeditor-note 为 attribute value Vue 自己创造了一套 template language。最基本的数据绑定是文本插值Text Interpolation它可以改变 element 的 content像这样
span{{ message }}/span这种语法被成为 “Mustache”语法 (即双大括号)。再结合上节讲到的 reactive object我们可以这样写
import { ref } from vueconst message ref(Hello World!)效果是这样的 可能你会有些疑问为什么不是写 {{message.value}}因为它是 top-level property会自动解包unwrapping
const object { id: ref(1) }比如这个里面id 就不是 top-level property如果你这么写
{{ object.id 1 }}渲染的结果将是 [object Object]1 你需要手动解包才会渲染出 2
{{ object.id.value 1 }}Directive
v-xxx 就是一种 attribute在它的 template language 中被称为 directive
v-bind
Attribute bind
在 Vue 中Mustache 语法只能用于文本插值来改变 element content没法儿操作 element attribute。而且 element attribute 是静态的为了给 element attribute 绑定一个动态值需要使用 Vue 的 v-bind directive
div v-bind:iddynamicId/div冒号后面的 id 被称为 directive 的参数argumentdynamicId 则为参数值它会和响应式对象的 property 同步。它可以简写为
div :iddynamicId/div例子
script setup
import { ref } from vueconst titleClass ref(title)
/scripttemplateh1 :classtitleClassMake me red/h1
/templatestyle
.title {color: red;
}
/stylev-on
Event Listen
可以通过 v-on 来监听 DOM event
button v-on:clickincrement{{ count }}/button简写
button clickincrement{{ count }}/button点击 button 会触发 increment() 这个函数。
例子
script setup
import { ref } from vueconst count ref(0)function increment() {count.value
}
/scripttemplatebutton clickincrementcount is: {{ count }}/button
/templatev-model
Form bind
同时使用 v-bind 和 v-on 对表单form进行绑定和监听
script setup
import { ref } from vueconst text ref()function onInput(e) {text.value e.target.value
}
/scripttemplateinput :valuetext inputonInput placeholderType herep{{ text }}/p
/template啊这么写实在太麻烦所以 vue 提供了 v-model。当然最好看一下它支持哪些 element。
script setup
import { ref } from vueconst text ref()
/scripttemplateinput v-modeltext placeholderType herep{{ text }}/p
/templatev-if and v-else
Conditional Rendering
v-if 和 v-else 可以根据条件来决定 element 是否在 DOM 中。
例子
script setup
import { ref } from vueconst awesome ref(true)function toggle() {awesome.value !awesome.value
}
/scripttemplatebutton clicktoggletoggle/buttonh1 v-ifawesomeVue is awesome!/h1h1 v-elseOh no /h1
/template改变 awesome 的值来显示 “Vue is awesome!” 和 “Oh no ”。
v-for
当你想写一个列表时一个个写列表的 element 实在太累了如果有 1000 个那不就完蛋了所以 v-for 可以通过循环直接渲染出列表当然你得给相应的数据
script setup
import { ref } from vue// 给每个 todo 对象一个唯一的 id
let id 0const newTodo ref()
const todos ref([{ id: id, text: Learn HTML },{ id: id, text: Learn JavaScript },{ id: id, text: Learn Vue }
])function addTodo() {todos.value.push({ id: id, text: newTodo.value })newTodo.value
}function removeTodo(todo) {todos.value todos.value.filter((t) t ! todo)
}
/scripttemplateform submit.preventaddTodoinput v-modelnewTodo required placeholdernew todobuttonAdd Todo/button/formulli v-fortodo in todos :keytodo.id{{ todo.text }}button clickremoveTodo(todo)X/button/li/ul
/templatecomputed()
template 里面可以写这种计算表达式
pHas published books:/p
span{{ author.books.length 0 ? Yes : No }}/spanconst author reactive({name: John Doe,books: [Vue 2 - Advanced Guide,Vue 3 - Basic Guide,Vue 4 - The Mystery]
})但是这种计算写在 template 里是真的不好理解个人感觉在结构上 View 里不能有逻辑所以尽量不要这么写。
所以我们可以用 computed()这样
script setup
import { reactive, computed } from vueconst author reactive({name: John Doe,books: [Vue 2 - Advanced Guide,Vue 3 - Basic Guide,Vue 4 - The Mystery]
})// 一个计算属性 ref
const publishedBooksMessage computed(() {return author.books.length 0 ? Yes : No
})
/scripttemplatepHas published books:/pspan{{ publishedBooksMessage }}/span
/template它和 method() 最大区别在于它是只有在响应式对象更新时才会重新被调用和计算否则就会直接返回缓存值即 books 不变重渲染比如页面刷新更新时 computed() 不会被调用而 method() 则会。插嘴一句 method()重渲染时总会被调用。
Lifecycle and Template Refs
手动用 JavaScript 操作 DOM 是一件苦差事所以我们用 vue 来帮忙但是有时我们不得不操作 DOM这个时候我们就得使用模板引用template ref
这个时候就要使用 ref attribute
p refpElementRefhello/p如果要访问这个 ref我们需要声明declareref 并初始化
const pElementRef ref(null)注意我们使用给 ref 的 argument 为 null这是因为script setup 执行时DOM 还没有初始化template ref 只能在挂在mount后访问所以我们可以使用生命周期钩子lifecycle hook比如 onMounted()关于 lifecycle 请看生命周期图示
例子
script setup
import { ref, onMounted } from vueconst pElementRef ref(null)onMounted(() {pElementRef.value.textContent mounted!
})
/scripttemplatep refpElementRefhello/p
/templatewatch()
watch() 可以监察一个 ref并触发一个 callback function比如下面的例子就是监察 todoId触发 fetchData
script setup
import { ref, watch } from vueconst todoId ref(1)
const todoData ref(null)async function fetchData() {todoData.value nullconst res await fetch(https://jsonplaceholder.typicode.com/todos/${todoId.value})todoData.value await res.json()
}fetchData()watch(todoId, fetchData)
/scripttemplatepTodo id: {{ todoId }}/pbutton clicktodoId :disabled!todoDataFetch next todo/buttonp v-if!todoDataLoading.../ppre v-else{{ todoData }}/pre
/templateComponent
Vue application 常常是多个 component 嵌套创建所以就有 parent component 包含 child component
如果要使用 child component就需要导入它
import ChildComp from ./ChildComp.vue使用 child component
ChildComp /例子
!--App.vue--script setup
import ChildComp from ./ChildComp.vue
/scripttemplateChildComp /
/templateProps
child component 可以通过 props 从 parent component 获取动态数据
!--ChildComp.vue--
script setup
const props defineProps({msg: String
})
/script注意defineProps() 是一个 runtime marcro不需要导入。 这样msg 就可以在 child component 的 template 中使用
templateh2{{ msg || No props passed yet }}/h2
/template而 parent component 则可以用 v-bind 传递数据
!--App.vue--
ChildComp :msggreeting /例子
!--App.vue--
script setup
import { ref } from vue
import ChildComp from ./ChildComp.vueconst greeting ref(Hello from parent)
/scripttemplateChildComp :msggreeting /
/template!--ChildComp.vue--
script setup
const props defineProps({msg: String
})
/scripttemplateh2{{ msg || No props passed yet }}/h2
/templatechild component 中采用的 “runtime declaration”还有一种是如果你用 typescript需要采用 “type-based declaration”具体看官方文档。
Emits
child component 可以向 parent component 传 eventemit() 中第一个 argument 是 event name其他的会传给 event listener。
parent component 可以通过 v-on 监听 child-emitted event并且可以将额外的 argument 赋值给 local state
!--App.vue--
script setup
import { ref } from vue
import ChildComp from ./ChildComp.vueconst childMsg ref(No child msg yet)
/scripttemplateChildComp response(msg) childMsg msg /p{{ childMsg }}/p
/template!--ChildComp.vue--
script setup
const emit defineEmits([response])emit(response, hello from child)
/scripttemplateh2Child component/h2
/templateSolots
除了 propsparent component 可以将 template 片段传给 child component而在 child component则可以使用 slot 来显示片段的内容。
!--App.vue--
script setup
import { ref } from vue
import ChildComp from ./ChildComp.vueconst msg ref(from parent)
/scripttemplateChildCompMessage: {{ msg }}/ChildComp
/templatetemplateslotFallback content/slot
/template以上 parent component 中ChildComp 的内容会传给 child component 中的 slot最后渲染在 parent component 上。 Essential
Create application
application instance and root component
Vue application 通常是通过 createApp 来创建一个 application instance
而 createApp 的参数则被称为 root component而且其他 component 则作为 root 的 child component所以 vue application 是由 root component 和 child component 组成的。
一般这种创建代码都在 project-name/src/main.js
!--main.js--
import { createApp } from vue
// 导入一个单组件
import App from ./App.vue
// 将这个单组件作为根组件
const app createApp(App)一个 Todo application 的例子
App (root component)
├─ TodoList
│ └─ TodoItem
│ ├─ TodoDeleteButton
│ └─ TodoEditButton
└─ TodoFooter├─ TodoClearButton└─ TodoStatisticsmount application
application instance 必须调用 mount() 才能渲染而必须的 argument 则为 DOM 的 element 或者 CSS selector
在 vue project 中一般在 project-name/index.html
div idapp/div注意 mount() 应该始终在应用配置完成后调用简单点儿说就是最后调用。 Applicaiton configuration
application instance 会提供一个 config object这样就可以配置 vue app比如 app-level optioncapture error
app.config.errorHandler (err) {/* 处理错误 */
}或者 component registration
比如 global registration
app.component(TodoDeleteButton, TodoDeleteButton)其他细节清参考 vue 文档。
Component registration
如果要使用 component则必须是 registered
Global registration
使用 .component() method
import { createApp } from vueconst app createApp({})app.component(// the registered nameMyComponent,// the implementation{/* ... */}
)如果使用 SFC则
import MyComponent from ./App.vueapp.component(MyComponent, MyComponent)Local registration
使用 component option
import ComponentA from ./ComponentA.jsexport default {components: {ComponentA},setup() {// ...}
}使用 SFC
script setup
import ComponentA from ./ComponentA.vue
/scripttemplateComponentA /
/templateToolchain
当你创建一个 vue project 时手动新建目录和文件是很麻烦的事情所有我们有项目脚手架Project Scaffolding来自动创建 project 基本的目录和文件。
Vite
尤雨溪开发的 build tool支持 SFC通过 Vite 创建项目
npm create vuelatestVue CLI
基于 webpack 的 build tool但现在是维护状态建议使用 Vite。 文章转载自: http://www.morning.ai-wang.cn.gov.cn.ai-wang.cn http://www.morning.csgwd.cn.gov.cn.csgwd.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.lsqmb.cn.gov.cn.lsqmb.cn http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.shprz.cn.gov.cn.shprz.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.pdkht.cn.gov.cn.pdkht.cn http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.pbmg.cn.gov.cn.pbmg.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.lqzhj.cn.gov.cn.lqzhj.cn http://www.morning.pqppj.cn.gov.cn.pqppj.cn http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.jfbrt.cn.gov.cn.jfbrt.cn http://www.morning.pwghp.cn.gov.cn.pwghp.cn http://www.morning.qnxkm.cn.gov.cn.qnxkm.cn http://www.morning.hphrz.cn.gov.cn.hphrz.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.wftrs.cn.gov.cn.wftrs.cn http://www.morning.qjzgj.cn.gov.cn.qjzgj.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.jwdys.cn.gov.cn.jwdys.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.bpmmq.cn.gov.cn.bpmmq.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.lxhny.cn.gov.cn.lxhny.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.wlstn.cn.gov.cn.wlstn.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.wjlbb.cn.gov.cn.wjlbb.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.stfdh.cn.gov.cn.stfdh.cn http://www.morning.jfmjq.cn.gov.cn.jfmjq.cn http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.pdmml.cn.gov.cn.pdmml.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn