This extension adds Vue3 Code Snippets into Visual Studio Code.html
这个插件基于最新的 Vue3 的 API 添加了 Code Snippets。vue
Including most of the API of Vue3. You can type reactive
, choose reactive
, and press ENTER, then const data = reactive({...})
appear on the screen.react
插件的 Snippets 以下表格所示,好比你能够键入 reactive
而后按上下键选中 reactive
再按 Enter 键,就输入了const data = reactive({...})
了。git
Prefix | JavaScript Snippet Content |
---|---|
import |
import {...} from "@vue/composition-api" |
import |
import {...} from 'vue' |
newVue |
newVue({...}) |
createComponent |
createComponent({...}) |
export |
export default { ... } |
setup |
setup(${...}) {...} |
reactive |
const data = reactive({...}) |
watch |
watch(..., ...) |
watchFn |
watch(() => {...}) |
computed |
computed(() => { get: () => {...}, set: () => {...}}) |
toRefs |
toRefs(...) |
ref |
ref(...) |
props |
props(...) |
onBeforeMount |
onBeforeMount(...) |
onMounted |
onMounted(...) |
onBeforeUpdate |
onBeforeUpdate(...) |
onUpdated |
onUpdated(...) |
onBeforeUnmount |
onBeforeUnmount(...) |
onUnmounted |
onUnmounted(...) |
onErrorCaptured |
onErrorCaptured(...) |
Vue Composition APIgithub
@vue/composition-api
使开发者们能够在 Vue 2.x
中使用 Vue 3.0
引入的基于函数的逻辑复用机制。vue-cli
English Versiontypescript
npmnpm
npm install @vue/composition-api --save
yarnvisual-studio-code
yarn add @vue/composition-api
CDNapi
<script src="https://unpkg.com/@vue/composition-api/dist/vue-composition-api.umd.js"></script>
By using the global variable window.vueCompositionApi.
经过全局变量 window.vueCompositionApi
来使用。
You must install @vue/composition-api via Vue.use() before using other APIs:
在使用任何 @vue/composition-api
提供的能力前,必须先经过 Vue.use()
进行安装:
import Vue from 'vue'; import VueCompositionApi from '@vue/composition-api'; Vue.use(VueCompositionApi);
After installing the plugin you can use the Composition API to compose your component.
安装插件后,您就能够使用新的 Composition API 来开发组件了。
This plugin requires TypeScript version >3.5.1. If you are using vetur
, make sure to set vetur.useWorkspaceDependencies
to true
.
To let TypeScript properly infer types inside Vue component options, you need to define components with createComponent
:
请使用最新版的 TypeScript,若是你使用了 vetur
,请将 vetur.useWorkspaceDependencies
设为 true
。
为了让 TypeScript 正确的推导类型,咱们必须使用 createComponent
来定义组件:
import { createComponent } from '@vue/composition-api'; const Component = createComponent({ // 启用类型推断 }); const Component = { // 没法进行选项的类型推断 // TypeScript 没法知道这是一个 Vue 组件的选项对象 };
🚀 An Example Repository with TS and TSX support is provided to help you start.
To support TSX, create a declaration file with following content in your project.
🚀 这里有一个配置好 TS/TSX 支持的示例仓库来帮助你快速开始.
要支持 TSX,请建立一个类型定义文件并提供正确的 JSX 定义。内容以下:
// file: shim-tsx.d.ts` import Vue, { VNode } from 'vue'; import { ComponentRenderProxy } from '@vue/composition-api'; declare global { namespace JSX { // tslint:disable no-empty-interface interface Element extends VNode {} // tslint:disable no-empty-interface interface ElementClass extends ComponentRenderProxy {} interface ElementAttributesProperty { $props: any; // specify the property name to use } interface IntrinsicElements { [elem: string]: any; } } }
Ref
UnwrapUnwrap
is not working with Array index.
数组索引属性没法进行自动的Unwrap
:
ref
as a direct child of Array
:Array
直接存取 ref
对象:const state = reactive({ list: [ref(0)], }); // no unwrap, `.value` is required state.list[0].value === 0; // true state.list.push(ref(1)); // no unwrap, `.value` is required state.list[1].value === 1; // true
ref
in a plain object when working with Array
:ref
的普通对象:const a = { count: ref(0), }; const b = reactive({ list: [a], // a.count will not unwrap!! }); // no unwrap for `count`, `.value` is required b.list[0].count.value === 0; // true
const b = reactive({ list: [ { count: ref(0), // no unwrap!! }, ], }); // no unwrap for `count`, `.value` is required b.list[0].count.value === 0; // true
ref
in a reactive
when working with Array:ref
存放到 reactive
对象中:const a = reactive({ count: ref(0), }); const b = reactive({ list: [a], }); // unwrapped b.list[0].count === 0; // true b.list.push( reactive({ count: ref(1), }) ); // unwrapped b.list[1].count === 1; // true
reactive
will mutate the origin objectreactive
会返回一个修改过的原始的对象This is an limitation of using Vue.observable
in Vue 2.
此行为与 Vue 2 中的 Vue.observable
一致
Vue 3 will return an new proxy object.
Vue 3 中会返回一个新的的代理对象.
watch()
APIonTrack
and onTrigger
are not available in WatchOptions.
不支持 onTrack
和 onTrigger
选项。
✅ Support ❌ Not Supported
✅ String ref && return it from setup()
:
<template> <div ref="root"></div> </template> <script> export default { setup() { const root = ref(null); onMounted(() => { // the DOM element will be assigned to the ref after initial render console.log(root.value); // <div/> }); return { root, }; }, }; </script>
✅ String ref && return it from setup()
&& Render Function / JSX:
export default { setup() { const root = ref(null); onMounted(() => { // the DOM element will be assigned to the ref after initial render console.log(root.value); // <div/> }); return { root, }; }, render() { // with JSX return () => <div ref="root" />; }, };
❌ Function ref:
<template> <div :ref="el => root = el"></div> </template> <script> export default { setup() { const root = ref(null); return { root, }; }, }; </script>
❌ Render Function / JSX in setup()
:
export default { setup() { const root = ref(null); return () => h('div', { ref: root, }); // with JSX return () => <div ref={root} />; }, };
If you really want to use template refs in this case, you can access vm.$refs
via SetupContext.refs
.
若是你依然选择在 setup()
中写 render
函数,那么你能够使用 SetupContext.refs
来访问模板引用,它等价于 Vue 2.x 中的 this.$refs
:
⚠️ Warning: The
SetupContext.refs
won't exist inVue 3.0
.@vue/composition-api
provide it as a workaround here.
⚠️ 警告:
SetupContext.refs
并不属于Vue 3.0
的一部分,@vue/composition-api
将其曝光在SetupContext
中只是临时提供一种变通方案。
export default { setup(initProps, setupContext) { const refs = setupContext.refs; onMounted(() => { // the DOM element will be assigned to the ref after initial render console.log(refs.root); // <div/> }); return () => h('div', { ref: 'root', }); // with JSX return () => <div ref="root" />; }, };
You may also need to augment the SetupContext
when working with TypeScript:
若是项目使用了 TypeScript,你还须要扩展 SetupContext
类型:
import Vue from 'vue'; import VueCompositionApi from '@vue/composition-api'; Vue.use(VueCompositionApi); declare module '@vue/composition-api/dist/component/component' { interface SetupContext { readonly refs: { [key: string]: Vue | Element | Vue[] | Element[] }; } }
Even if there is no definitive Vue 3 API for SSR yet, this plugin implements the onServerPrefetch lifecycle hook that allows you to use the serverPrefetch hook found in the classic API.
import { onServerPrefetch } from '@vue/composition-api'; export default { setup (props, { ssrContext }) { const result = ref(); onServerPrefetch(async () => { result.value = await callApi(ssrContext.someId); }); return { result, }; }, };
若是文章和笔记能带您一丝帮助或者启发,请不要吝啬你的赞和 Star,你的确定是我前进的最大动力😁