Skip to content

Commit

Permalink
feat: 支持H5导入所有组件
Browse files Browse the repository at this point in the history
  • Loading branch information
jry committed Aug 18, 2024
1 parent cf29935 commit 4f37654
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/uni_modules/uview-plus/components/u-copy/u-copy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</template>
<script>
export default {
name: "xy-copy",
name: "up-copy",
props: {
content: {
type: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
* @example <u-datetime-picker :show="show" :value="value1" mode="datetime" ></u-datetime-picker>
*/
export default {
name: 'datetime-picker',
name: 'up-datetime-picker',
mixins: [mpMixin, mixin, props],
data() {
return {
Expand Down
39 changes: 39 additions & 0 deletions src/uni_modules/uview-plus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,46 @@ const $u = {
platform
}

export const mount$u = function() {
uni.$u = $u
}

// #ifdef H5
const importFn = import.meta.glob('./components/u-*/u-*.vue', { eager: true })
let components = [];

// 批量注册全局组件
for (const key in importFn) {
let component = importFn[key].default;
if (component.name && component.name.indexOf('u--') !== 0) {
const name = component.name.replace(/u-([a-zA-Z0-9-_]+)/g, 'up-$1');
component.name = name;
console.log('component', component.name)
component.install = function (Vue) {
Vue.component(component.name, component);
};

// 导入组件
components.push(component);
}
}
// #endif

function toCamelCase(str) {
return str.replace(/-([a-z])/g, function(match, group1) {
return group1.toUpperCase();
}).replace(/^[a-z]/, function(match) {
return match.toUpperCase();
});
}

const install = (Vue) => {
// #ifdef H5
components.forEach(function(component) {
Vue.component(component.name, component);
});
// #endif

// 同时挂载到uni和Vue.prototype中
// $u挂载到uni对象上
uni.$u = $u
Expand Down

0 comments on commit 4f37654

Please sign in to comment.