<template> <TestComponent> <!-- Дефолтный scoped slot --> <div slot-scope="{ message }"> {{ `Default slot with message: ${message}` }} </div> <!-- Именованный scoped slot --> <div slot="text" slot-scope="{ text }"> {{ `Named slot with text: ${text}` }} </div> </TestComponent> </template>
<template> <TestComponent> <!-- Дефолтный scoped slot --> <template v-slot="{ message }"> <div> {{ `Default slot with message: ${message}` }} </div> </template> <!-- Именованный scoped slot --> <template v-slot:text="{ text }"> <div> {{ `Named slot with text: ${text}` }} </div> </template> </TestComponent> </template>
<template> <!-- v-slot используется прямо на родителе --> <TestComponent v-slot="{ message }"> <div> {{ `Default slot with message: ${message}` }} </div> </TestComponent> </template>
<template> <TestComponent> <!-- # - это сокращение для v-slot: --> <template #text="{ text }"> <div> {{ `Named slot with text: ${text}` }} </div> </template> </TestComponent> </template>
<div v-bind="{ [key]: value }"></div> <div v-on="{ [event]: handler }"></div>
<div v-bind:[key]="value"></div> <div v-on:[event]="handler"></div>
<template> <div> <!-- v-bind с динамическим ключом --> <div v-bind:[key]="value"></div> <!-- сокращение v-bind с динамическим ключом --> <div :[key]="value"></div> <!-- v-on с динамическим событием --> <div v-on:[event]="handler"></div> <!-- сокращение v-on с динамическим событием --> <div @[event]="handler"></div> <!-- v-slot с динамическим именем --> <TestComponent> <template v-slot:[name]> Hello </template> </TestComponent> <!-- сокращение v-slot с динамическим именем --> <TestComponent> <template #[name]> Cool slot </template> </TestComponent> </div> </template>
import Vue from vue; const state = Vue.observable({ counter: 0, }); export default { render() { return ( <div> {state.counter} <button v-on:click={() => { state.counter++; }}> Increment counter </button> </div> ); }, };
<template> <div v-if="item"> {{ item.name }} </div> </template> <script> export default { // Вызовется на сервере async serverPrefetch() { await this.fetchItem(); }, computed: { item() { return this.$store.state.item; }, }, // Вызовется на клиенте mounted() { if (!this.item) { this.fetchItem(); } }, methods: { async fetchItem() { await this.$store.dispatch('fetchItem'); }, }, }; </script>
/* Упрощенный entry-server.js */ import { createApp } from './app'; export default context => new Promise((resolve, reject) => { const { app, router, store } = createApp(); const { url } = context; router.push(url); router.onReady(() => { context.rendered = () => { // Передаем состояние хранилища после завершения всех serverPrefetch() context.state = store.state; }; resolve(app); }, reject); });
<template> <div> <template key="test-key"> {{ message }} </template> </div> </template>
Error compiling template: <div> <template key="test-key"> {{ message }} </template> </div> - <template> cannot be keyed. Place the key on real elements instead.
Errors compiling template: <template> cannot be keyed. Place the key on real elements instead. 1 | 2 | <div> 3 | <template key="test-key"> | ^^^^^^^^^^^^^^ 4 | {{ message }} 5 | </template>
/* TestComponent.vue */ <template> <div @click="doSomething()"> Some message </div> </template> <script> export default { methods: { async doSomething() { await this.$nextTick(); throw new Error('Another Error'); }, }, async mounted() { await this.$nextTick(); throw new Error('Some Error'); }, }; </script>
[Vue warn]: Error in mounted hook (Promise/async): "Error: Some Error"
[Vue warn]: Error in v-on handler (Promise/async): "Error: Another Error"
<html lang="en"> <head> <title>Document</title> </head> <body> <div id="app"> {{ message }} </div> <script type="module"> // Раньше приходилось использовать vue.esm.js, // который содержал транспилированный код, // весил чуть больше и работал чуть медленнее import Vue from 'path/to/vue.esm.browser.js'; new Vue({ el: '#app', data() { return { message: 'Hello World!', }; }, }); </script> </body> </html>
<template> <div> <div v-bind:textContent.prop="'Important text content'" /> <!-- Или сокращенный вариант --> <div :textContent.prop="'Important text content'" /> </div> </template>
<template> <div .textContent="'Important text content'" /> </template>
/* TestComponent.vue */ <template> <div> {{ message }} </div> </template> <script> export default { data() { return { message: { value: 'qwerty', toString() { return 'Hello Habr!'; }, }, }; }, }; </script>
{ "value": "qwerty" }
Hello Habr!
/* TestComponent.vue */ <template> <div> <div v-for="item in items" :key="item" > {{ item }} </div> </div> </template> <script> export default { data() { return { items: new Set([4, 2, 6]), }; }, }; </script>
npm i vue@2.6.0-beta.3
npm i vue-template-compiler@2.6.0-beta.3 vue-server-renderer@2.6.0-beta.3
Source: https://habr.com/ru/post/437582/