Construct a demo framework (VUE3)
- vue-router
- rollup-context
Reactivity API: Core |
/src/examples/
// import { createVNode, h } from "vue" export const HelloWorld = () => { // return h("h1", ["Hello world!!!"]) return <h1>Hello world!!!</h1> }
/src/
// import type only introduces type and will not executeimport type { RouteRecordRaw } from "vue-router"; //Route typetype MyRouteType = RouteRecordRaw & { key: string }
/src/
import { createApp } from 'vue' import { createWebHashHistory, createRouter } from 'vue-router' import App from './App' import { MyRouteType } from './mytypes' // rollup-context capability, webpack does not have// The framework is vite3// Get all .tsx【typescript + jsx】 files under /examplesconst examples = ("./examples/**/*.tsx") // Install a vue-router@next// npm add vue-router@next const routes: MyRouteType[] = [] const examplePromises = (examples) .map(x => examples[x]) .map(f => f()) // All examplePromises are resolved(examplePromises) .then(list => { for (let module of list) { for (let key in module) { const Component = module[key] ({ path: "/" + (), key, component: Component }) } } const router = createRouter({ history: createWebHashHistory(), routes }) // Pass routes as the App property const app = createApp(App, { routes }) (router) ('#app') })
/src/
import { RouterLink, RouterView } from "vue-router" import { MyRouteType } from "./mytypes" import "./" export default (props: { routes: MyRouteType[] }) => { return <> <header><h2>Project practical examples</h2></header> <div class="body"> <ul class="menu"> {(x => { return <li key={}> <RouterLink to={}>{}</RouterLink> </li> })} </ul> <div class="content"> <RouterView /> </div> </div> </> }
/src/
* { margin : 0; } html, body { height : 100%; } #app { height: 100%; } header { height : 60px; line-height: 60px; padding-left: 20px; width : 100%; background-color: black; color : white; } .body { display: flex; width : 100%; height : 100%; } .menu { padding-top : 20px; width : 200px; border-right: 1px solid #f2f3f4; min-height: 100%; } .menu li a{ text-decoration: none; color : #2377de; } .menu li a:visited { text-decoration: none; color : #2377de; } .menu li { list-style: none; } .content { margin : 10px; position: relative; }
/src/examples/
import { defineComponent, PropType, Ref, ref } from "vue"; // Use responsive values to create through defineComponent,// If you do not use the responsive value RefExample01 = ()= { return <div>2233</div> }// Without using defineComponent, there is no setup() to useexport const RefExample01 = defineComponent({ setup() { // ref is a proxy, gets the value through .value const count = ref(0) ('The setup function is executed only once') // Rendering function return () => { ('render function executes every time') return ( <div> <button onClick={() => { ++ }}>+</button> {} </div> ) } } }) export const RefExample02 = defineComponent({ setup() { // ref is a proxy, gets the value through .value const count = ref(0) ('The setup function is executed only once') // Rendering function return () => { ('render function executes every time') return ( <div> <button onClick={() => { ++ }}>+</button> </div> ) } } }) export const RefExample03 = defineComponent({ setup() { // ref is a proxy, gets the value through .value const count = ref(0) ('The setup function is executed only once') // Rendering function return () => { return ( <div> <button onClick={() => { ++ }}>+</button> <Count1 count={count} /> <Counter count={count} /> </div> ) } } }) const Counter = ({ count }: { count: Ref<number> }) => { return <div>{}</div> } const Count1 = defineComponent({ props: { // Mapping attributes are required count: { type: Object as PropType<Ref<number>>, // Give an alias type required: true // Write this, vue can let you get the value in render } }, setup(props) { return ( // You can get value by writing this way // props: { // count: Ref<number> // } ) => { return <div>{}</div> } } })
/src/examples/
import { customRef, defineComponent, reactive, toRefs, unref, isRef, } from "vue"; // /api/ export const ReactiveExample01 = defineComponent({ setup() { const state = reactive({ a: "123", b: 2 }) setTimeout(() => { = "456" }, 1000) setTimeout(() => { = 100 }, 2000) return () => { return <div> <div>{}</div> <div>{}</div> </div> } } }) function getState() { const state = reactive({ a: "123", b: 2 }) return toRefs(state) } export const ReactiveExample02 = defineComponent({ setup() { const { a, b } = getState() // const { a, b } = toRefs(state) // batch ref setTimeout(() => { // = "456" // reactive usage = "456" // Ref usage }, 1000) setTimeout(() => { // = 100 = 100 }, 2000) return () => { return <div> <div>{}</div> <div>{}</div> </div> } } }) // Anti-shake customReffunction useDebouncedRef(value: string, delay = 200) { let timeout: number | undefined return customRef((track: () => void, trigger: () => void) => { return { get() { track() return value }, set(newValue: any) { clearTimeout(timeout) timeout = setTimeout(() => { value = newValue trigger() }, delay) } } }) } export default { setup() { return { text: useDebouncedRef('hello') } } }
think
- What depends on Reactive value? It is our rendering that depends on them
- Why not explicitly specify the dependency of Reactive value?
Embrace functional, there is no way to explicitly provide dependencies. We are not sure about the value, we know when setting up, but when rendering, there is no way to give it dependencies.
What is the difference between the Reactive mode provided by Vue?
const state = ({ count: 0 }) const Demo = { render(h) { return h('button', { on: { click: () => { ++ }} }, `count is: ${}`) } }
The difference is the function
- What is Observable? The value of each ref() that can be observed
- Reactive === Observable ?
Reactive [Responsive, this component, this value, at any time, like a human, has active thinking, knows how to respond to external environments, and knows how to do it]
Observable [Design mode, architectural means, are to observe things that can be observed]
This is the end of this article about Vue3's API interpretation - ref and reactive. For more related vue3 ref and reactive content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!