感謝滾動君大佬提供的思路,但是此方法只適用于微信小程序
app端之所以不能用的關(guān)鍵原因在于不能找到resourcePath原始路徑,不像小程序會提供一個路徑區(qū)分哪些文件來自組件哪些文件來自頁面,如果不做區(qū)分,那么頁面棧就會超載,無法顯示。
既然它自身不能提供路徑,那么我們可以修改源碼,路徑在D:\Hbuild\plugins\uniapp-cli\node_modules@dcloudio\vue-cli-plugin-uni\packages\vue-loader\lib\loaders\templateLoader.js
的templateLoader.js文件中修改代碼如圖:
在compilerOptions方法體中的最后一行加上
resourcePath: this.resourcePath
注意上一行的逗號不要忘了
然后在項目里面新建一個文件vue.config.js
填以下內(nèi)容
module.exports = {
chainWebpack: config => {
config.module.rule('vue').use('vue-loader').loader('vue-loader').tap(options => {
const compile = options.compiler.compile
options.compiler.compile = (template,...args) => {
//用來區(qū)分組件還是頁面
if(args[0].resourcePath.search("pages")!=-1){
//防止app端重復(fù)加載而重復(fù)添加組件
if(template.search("<upload_file/>")==-1){
template = template.replace(/[\s\S]+?<[\d\D]+?>/, _ => `${_}
<‘upload_file ’/>
`)
}
}
return compile(template,...args)
}
return options
})
}
}
差點忘了你的組件必須是全局掛載的,在mian.js里面添加
import upload_file from '@/components/upload_file.vue'
Vue.component('upload_file', upload_file)
下面我自定義懸浮組件的源碼
upload_file.vue
<template>
<view>
<uni-fab :pattern="pattern" :horizontal="horizontal" :vertical="vertical" @fabClick="upload_data" ></uni-fab>
</view>
</template>
<script>
export default {
name: "",
data() {
return {
vertical: 'bottom',
horizontal: 'right',
pattern: {
color: '#7A7E83',
backgroundColor: '#fff',
selectedColor: '#007AFF',
buttonColor: '#007AFF',
iconColor: '#fff',
icon:'upload-filled'
},
}
},
methods:{
upload_data(){
this.$company.common({
}, {
'api':'unline'
}).then(e => {
console.log(e)
})
}
}
}
</script>
<style lang="scss" scoped>
/deep/ .uni-fab__circle--rightBottom[data-v-137a85dc] {
right: 15px;
bottom: 30px;
right: calc(70rpx + var(--window-right));
bottom: calc(150rpx + var(--window-bottom));
}
</style>
都填完后重新加載一遍文章來源:http://www.zghlxwxcb.cn/news/detail-532654.html
注:如果是需要在小程序運行需要將該文件復(fù)原。文章來源地址http://www.zghlxwxcb.cn/news/detail-532654.html
到了這里,關(guān)于【解決】uniapp的全局公共組件(不用在每個頁面引入組件標(biāo)簽),各平臺均可使用!?。?!的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!