javascript - Inherit props and functions from vue parent instance
问题描述
I am trying to define some default props and functions in my vue instance that i want to reuse in all of my components. How do i go about doing this? I have tried to pass the props but since they are read only it wont work.
Am i approaching this in the right way or should i look at another way?
// vue.js
import { createApp } from 'vue';
import Page from './vue-instances/Page';
const Vue = createApp({
data() {
return {
loading: false // default prop
};
},
components: {
Page
}
});
Vue.mount('#vue-app');
// Page.vue
export default {
props: { loading: Boolean },
data() {
return {
title: 'New Page',
};
},
mounted: function () {
setTimeout(() => {
this.loading = true; // Reuse inherited props
}, 1000);
}
};
解决方案
我认为你的方法不是最好的。如果您尝试出于某种目的重用数据(上述道具),请尝试创建一个使用该数据的组件并重用该组件,这将是一种更自然和内联的 UI 开发方式。
推荐阅读
- matlab - 使用 Matlab 中的动态规划在 2D 矩阵中的最大和路径
- sql - Redshift - 对数据进行增量更新
- reactjs - 为什么 Mapbox gl 仅在 React 中通过 setStyle 调用更改其样式而不更改状态?
- c# - 在 C# 中更快地调用 PowerShell cmdlet
- angular - 添加带有点符号选项的组件 Input():attr.src、style.padding.px
- bash - tee 扩展换行符 (\n)
- spring-data-jpa - 根据应用于实体的特征向 Spring 的 JpaRepository 添加动态行为
- c# - 从屏幕获取鼠标位置
- angular - Angular 6 反应式表单控制
- c# - 无事务回滚 SQL