yulimchen / blog Goto Github PK
View Code? Open in Web Editor NEW📖 Issues 博客,记录踩坑。
📖 Issues 博客,记录踩坑。
最近对简陋的 vant H5 项目模板进行了 Vue3 的升级,在此记录一下过程~
new Vue
// Vue 2 main.js
import Vue from 'vue'
import App from './App.vue'
//...
new Vue({
render: h => h(App)
}).$mount('#app')
// Vue 3 main.js
import { createApp } from 'vue'
import App from './App.vue'
//...
const app = createApp(App)
app.mount('#app')
// Vue 2 main.js
import Vue from 'vue'
import {
Button
} from 'vant'
Vue.component('van-button', Button) // Vue.use(Button)
// Vue 3 main.js
import { createApp } from 'vue'
import App from './App.vue'
import {
Button
} from 'vant'
const app = createApp(App)
app.component('van-button', Button) // app.use(Button)
app.mount('#app')
稍微封装一下
// src/plugins/registerVant.js
import {
Field,
Cell,
CellGroup,
Button,
Icon
} from 'vant'
const componentList = [
Field,
Cell,
CellGroup,
Button,
Icon
]
export function registerVantComp(app) {
componentList.forEach((comp) => {
app.use(comp)
})
}
// main.js
// ...
import { registerVantComp } from '@/plugins/registerVant'
registerVantComp(app)
v-on="$listeners"
来接收事件,但是在 Vue3 中 $listeners
已被移除,事件和属性统一用 v-bind="$attrs"
来接收,详情见文档。<!-- Vue2 -->
<div v-on="$listeners"/>
<!-- Vue3 -->
<div v-bind="$attrs" />
Composition API 应该是 Vue3 最受关注的新特性了,使用 Composition API 的话就要和熟悉的 this
说拜拜了。(当然在 Vue3 继续使用以前的 Optiosn API 也是可以的)
// vue2
export default {
name: 'Demo',
props: {
foo: {
type: String,
default: '111'
}
},
data() {
return {
bar: 123
}
},
mounted() {
console.log(this.foo) // '111'
console.log(this.bar) // 123
this.$emit('emitEvent', this.bar)
}
}
setup 方法第一个参数为 props
,第 2 个参数 ctx
接近 Vue2 中的 this
,但不完全相等!只是选择性地暴露部分属性,主要有 attrs
,emit
,slots
。
// vue3
import { onMounted } from 'vue'
export default {
name: 'About',
props: {
foo: {
type: String,
default: '111'
}
},
setup(props, ctx) {
const bar = 123
onMounted(() => {
console.log(props.foo) // '111'
console.log(bar) // 123
ctx.emit('emitEvent', bar)
})
return {
bar
}
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.