Ai
1 Star 4 Fork 0

李金文/vue-next学习

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
props.js 1.61 KB
Copy Edit Raw Blame History
李金文 authored 2019-12-08 11:17 +08:00 . first commit
const { createComponent, createApp, reactive } = Vue
// 计数器组件
const Counter = {
props: {
initCount: {
type: Number,
default: 0
}
},
template: `
<div class="counter-display">
<span class="counter-label">恭喜你,你已经写了</span>
<span class="counter-text">{{ state.count }}</span>
<span class="counter-label">斤代码!</span>
</div>
<div class="counter-btns">
<button class="btn" @click="increase">写一斤</button>
<button class="btn" @click="reset">删库啦</button>
</div>
`,
setup(props) {
const countOps = useCount(props.initCount)
return { ...countOps }
}
}
function useCount(initCount) {
const state = reactive({ count: initCount })
console.log("state reactive", state)
const increase = () => { state.count++ }
const reset = () => { state.count = 0 }
return { state, increase, reset }
}
// 创建一个 跟组件 app
const App = createComponent({
// 这个就相对于 在另一个 .vue 文件 引用 Counter 组件,需要在 components 属性局部注册组件
components: {
Counter,
},
// 挂载到 App 模板中
template: `
<div class="container">
<h3>计数器示例</h3>
<Counter />
</div>
`
})
// 启动
// container 就是相当于 new Vue() 中 el 元素
const container = document.querySelector("#app")
// createApp() 就是相当于 new Vue() 内部返回的就是 new Vue()
const app = createApp()
// 挂载组件
app.mount(App, container)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/kennana/vue_next_learning.git
git@gitee.com:kennana/vue_next_learning.git
kennana
vue_next_learning
vue-next学习
master

Search