Vue提供了一些内置的组件,这些组件可以在Vue应用中直接使用,无需额外安装或配置。以下是一些常见的Vue内置组件
name 属性
name 属性用于指定过渡的名称,它在定义过渡样式和钩子函数时非常有用。通过为过渡样式类名添加 name 的前缀,可以确保不同过渡之间的样式不会相互冲突。同时,name 也用于在过渡钩子函数中标识当前过渡的名称,以便在需要时进行特定处理。
tag 属性
tag 属性用于指定过渡组件渲染的标签。默认情况下,
- <template>
- <div>
- <button @click="addItem">Add Item</button>
- <button @click="removeItem">Remove Item</button>
-
- <transition-group name="a" tag="ul">
- <li v-for="item in items" :key="item.id">
- {{ item.text }}
- </li>
- </transition-group>
- </div>
- </template>
-
- <script>
- export default {
- data() {
- return {
- items: [
- { id: 1, text: "Item 1" },
- { id: 2, text: "Item 2" },
- { id: 3, text: "Item 3" },
- ],
- nextId: 4,
- };
- },
- methods: {
- addItem() {
- this.items.push({ id: this.nextId, text: `Item ${this.nextId}` });
- this.nextId++;
- },
- removeItem() {
- this.items.pop();
- },
- },
- };
- </script>
-
- <style>
- /*
- 激活时的过渡效果
- a 为 name定义的值
- -enter-active这部分写死
-
- 过渡效果需要自己写
- */
- .a-enter-active,
- .a-leave-active {
- transition: opacity 0.5s;
- }
- .a-enter,
- .a-leave-to {
- opacity: 0;
- }
- </style>
is
is 属性是
通过使用 is 属性,我们可以实现动态组件的渲染,即根据数据的变化在运行时选择不同的组件进行渲染。这对于根据用户的操作或其他条件切换不同的视图非常有用
根据选择动态渲染对应的组件
- <template>
- <div>
- <select v-model="selectedComponent">
- <option value="ComponentA">Component A</option>
- <option value="ComponentB">Component B</option>
- <option value="ComponentC">Component C</option>
- </select>
- <component :is="selectedComponent"></component>
- </div>
- </template>
-
- <script>
- import ComponentA from './ComponentA.vue';
- import ComponentB from './ComponentB.vue';
- import ComponentC from './ComponentC.vue';
-
- export default {
- data() {
- return {
- selectedComponent: 'ComponentA'
- };
- },
- components: {
- ComponentA,
- ComponentB,
- ComponentC
- }
- }
- </script>
组件用于缓存动态组件,以便在组件切换时保留其状态或避免重新渲染。它会缓存被包裹的组件的实例,并在组件切换时保持实例的状态,以提高应用的性能和响应性
- <template>
- <div>
- <select v-model="selectedComponent">
- <option value="ComponentA">Component A</option>
- <option value="ComponentB">Component B</option>
- <option value="ComponentC">Component C</option>
- </select>
- <keep-alive>
- <component :is="selectedComponent"></component>
- </keep-alive>
- </div>
- </template>
-
- <script>
- import ComponentA from './ComponentA.vue';
- import ComponentB from './ComponentB.vue';
- import ComponentC from './ComponentC.vue';
-
- export default {
- data() {
- return {
- selectedComponent: 'ComponentA'
- };
- },
- components: {
- ComponentA,
- ComponentB,
- ComponentC
- }
- }
- </script>
-
-
- export default {
-
- activated() {
- // activated 每次进入缓存也都都会执行
- },
- deactivated() {
- // 缓存组件被销毁时调用
- }
- }
它可以将一个组件内部的一部分模板“传送”到该组件的 DOM 结构外层的位置去。这类场景最常见的例子就是全屏的模态框
to
指定传送的目标。to 的值可以是一个 CSS 选择器字符串,也可以是一个 DOM 元素对象。
- <button @click="open = true">Open Modal</button>
-
- <Teleport to="body">
- <div v-if="open" class="modal">
- <p>Hello from the modal!</p>
- <button @click="open = false">Close</button>
- </div>
- </Teleport>
插糟同插槽那节课