getting start
快速上手
依赖要求
本文会帮助你从头使用vue2-context。
- 安装vue2-context
_3yarn add vue2-context -S_3// 或_3npm install vue2-context -S
- 创建上下文实例
- 父级组件
_24<template>_24 <ChatContextProvider :value="hello">_24 parent: {{ hello.val }}_24 <Child />_24 <button @click="hello.val++">++</button>_24 </ChatContextProvider>_24</template>_24 _24<script>_24import Child from './Child.vue';_24import { chatContext } from './config';_24const ChatContextProvider = chatContext.Provider;_24_24export default {_24 components: { ChatContextProvider, Child },_24 data() {_24 return {_24 hello: {_24 val: 0_24 }_24 }_24 },_24}_24</script>
- 子级组件
_17<template>_17 <div>_17 child : {{ context.value.val }}</div>_17</template>_17 _17<script>_17import { useContext } from 'vue2-context';_17import { chatContext } from './config';_17_17export default {_17 data() {_17 return {_17 context: useContext(chatContext, this)_17 }_17 },_17}_17</script>