两种风格的API简介 使用选项式 API,我们可以用包含多个选项的对象来描述组件的逻辑,例如 data
、methods
和 mounted
。选项所定义的属性都会暴露在函数内部的 this
上,它会指向当前的组件实例。
通过组合式 API,我们可以使用导入的 API 函数来描述组件逻辑。在单文件组件中,组合式 API 通常会与 `` 搭配使用。这个 setup
attribute 是一个标识,告诉 Vue 需要在编译时进行一些处理,让我们可以更简洁地使用组合式 API。比如,<script setup>
中的导入和顶层变量/函数都能够在模板中直接使用。
HelloWorld 选项式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <script> // import AlertBox from './components/AlertBox.vue' export default { data(){ return{ message:"HelloWorld" } } } </script> <template> {{message}} </template>
组合式 1 2 3 4 5 6 7 8 9 10 11 <script setup> import { ref } from 'vue' // “ref”是用来存储值的响应式数据源。 // 理论上我们在展示该字符串的时候不需要将其包装在 ref() 中, // 但是在下一个示例中更改这个值的时候,我们就需要它了。 const message = ref('Hello World!') </script> <template> <h1>{{ message }}</h1> </template>
样式绑定即更新 点击更新样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <!-- 现在我们将元素的 attribute / property 响应式地绑定到状态上。 这个 :title 语法是 v-bind:title 的简写。 --> <script setup> import { ref } from 'vue' const message =new ref( "Hello World!" ) const isRed = new ref(true) //更改文字颜色 function changeColor(){ isRed.value = !isRed.value } </script> <template> <p>样式绑定如下</p> <p :title="message">看!我有一个动态绑定标题</p> <p :class="{red:isRed}" @click="changeColor">点我切换颜色!(红/黑)</p> </template> <style> .red { color: red; } </style>
条件与循环 我们可以通过 v-if 和 v-for 指令条件性地或循环地渲染内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <script setup> import { ref } from 'vue' const show = ref(true) const list = ref([1, 2, 3]) </script> <template> <button @click="show = !show">Toggle List</button> <button @click="list.push(list.length + 1)">Push Number</button> <button @click="list.pop()">Pop Number</button> <button @click="list.reverse()">Reverse List</button> <ul v-if="show && list.length"> <li v-for="item of list">{{ item }}</li> </ul> <p v-else-if="list.length">List is not empty, but hidden.</p> <p v-else>List is empty.</p> </template>
表单绑定 我们可以使用 v-model 指令在状态和表单输入之间创建双向绑定。v-bind(快捷:=)可以实现单向数据绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 <script setup> import { ref } from 'vue' const text = ref('Edit me') const checked = ref(true) const checkedNames = ref(['Jack']) const picked = ref('One') const selected = ref('A') const multiSelected = ref(['A']) </script> <template> <h2>Text Input</h2> <input v-model="text"> {{ text }} <h2>Checkbox</h2> <input type="checkbox" id="checkbox" v-model="checked"> <label for="checkbox">Checked: {{ checked }}</label> <!-- 多个复选框可以绑定到 相同的 v-model 数组 --> <h2>Multi Checkbox</h2> <input type="checkbox" id="jack" value="Jack" v-model="checkedNames"> <label for="jack">Jack</label> <input type="checkbox" id="john" value="John" v-model="checkedNames"> <label for="john">John</label> <input type="checkbox" id="mike" value="Mike" v-model="checkedNames"> <label for="mike">Mike</label> <p>Checked names: <pre>{{ checkedNames }}</pre></p> <h2>Radio</h2> <input type="radio" id="one" value="One" v-model="picked"> <label for="one">One</label> <br> <input type="radio" id="two" value="Two" v-model="picked"> <label for="two">Two</label> <br> <span>Picked: {{ picked }}</span> <h2>Select</h2> <select v-model="selected"> <option disabled value="">Please select one</option> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ selected }}</span> <h2>Multi Select</h2> <select v-model="multiSelected" multiple style="width:100px"> <option>A</option> <option>B</option> <option>C</option> </select> <span>Selected: {{ multiSelected }}</span> </template>
简单组件的实现 TodoItem.vue 1 2 3 4 5 6 7 8 9 <script setup> const props = defineProps({ todo: Object }) </script> <template> <li>{{ todo.text }}</li> </template>
App.vue 在子组件调用const props=defineProps({})可以传入父组件中的一些数据信息。 选项式API中,直接定义props:[]传入信息是等价的方式。TypeScript 用户请参考:为组件 props 标注类型 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <!-- 这里展示了最简单的组件,它接收一个 prop 并渲染出来。 在指南页面了解更多关于组件的内容! --> <script setup> import { ref } from 'vue' import TodoItem from './TodoItem.vue' const groceryList = ref([ { id: 0, text: 'Vegetables' }, { id: 1, text: 'Cheese' }, { id: 2, text: 'Whatever else humans are supposed to eat' } ]) </script> <template> <ol> <!-- 我们给每个 todo 项提供它所表示的 todo 对象, 以便能够动态展示内容。 同时还需要给每个组件提供一个“key”, 这在指南的 v-for 部分有详细解释。 --> <TodoItem v-for="item in groceryList" :todo="item" :key="item.id" ></TodoItem> </ol> </template>