vue3项目如何在prototype原型上挂载方法并调用
vue3在main.js中挂载// main.jsimport { createApp } from 'vue'import App from './App.vue'import router from './router'import store from './store'const flag = function IsPC() {var userAgentInfo = navigator.u
·
vue3
在main.js中挂载
// main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
const flag = function IsPC() {
var userAgentInfo = navigator.userAgent
var Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad']
var flag = true
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false
break
}
}
return flag
}
// 必须给createdApp(App)赋值,如果直接写createApp(App).config挂载的话会报错,至于为啥,我不想知道
const app = createApp(App)
// 挂载到原型用config.globalProperties
app.config.globalProperties.$flag = flag
app.use(store).use(router).use(vant).use(ElementPlus).mount('#app')
在setup中调用
<script>
// 必须引入
import { getCurrentInstance } from 'vue'
export default {
name: 'travelTravel',
setup() {
const { proxy } = getCurrentInstance()
console.log(proxy.$flag()) // 此时就能打印return 的flag的值
},
}
</script>
<style lang="scss" scoped></style>
更多推荐
已为社区贡献7条内容
所有评论(0)