设置和访问Vue.prototype值

时间:2019-08-13 09:19:54

标签: javascript vue.js vuejs2

我正在尝试使用Vue.prototype在全局设置属性,但遇到了一些麻烦

我有以下内容:

microsoftTeams.initialize();
microsoftTeams.getContext((context) => {
    Vue.prototype.$teamsContext = true;
});


new Vue({
    el: '#overview',
    components: {
        Index
    },
    data: {
       // add attributes here
    },
});

我猜测这将阻止我在Vue.prototype调用中使用getContext,因为从未设置$teamsContext

有没有办法解决这个问题?

谢谢

编辑::

我添加了更多代码。

Index方法的created()组件中,我正在执行console.log("CONTEXT", this.$teamsContext),它返回CONTEXT undefined

1 个答案:

答案 0 :(得分:0)

非常有趣的问题。

假设#requires -Version 4 param( [string] $start = '\\Guru\Archive', [int] $thresholdDays = 15 ) # getting the name wasn't useful. keep objects as objects foreach ($folder in Get-ChildItem -Path $start -Directory) { "Processing Folder: $folder" # get all items once $folders, $files = ($folder | Get-ChildItem -Recurse). Where({ $_.PSIsContainer }, 'Split') # process files $files.Where{ $_.LastWriteTime -lt (Get-Date).AddDays(-$thresholdDays) } | Remove-Item -Force # process folders $folders.Where{ $_.Name -notin 'Inbound', 'Outbound' -and ($_ | Get-ChildItem).Count -eq 0 } | Remove-Item -Force } "Complete!" 是异步的,我的方法是在getContext准备就绪后加载(装载)应用程序。

这是实现:

getContext

Here是一个实时示例。

NB

由于您的异步操作可能是通过// main.js import Vue from "vue"; import App from "./App.vue"; Vue.config.productionTip = false; let app; const onResourceLoaded = () => { app.$mount("#app"); }; const littlePlugin = { async install(Vue, options) { // Add here your async action const resp = await fetch("https://jsonplaceholder.typicode.com/todos/1"); const parsedResp = await resp.json(); // Add global property after the action is ready Vue.prototype.$name = parsedResp.title; // Mount the app onResourceLoaded(); } }; Vue.use(littlePlugin); app = new Vue({ render: h => h(App) }); 完成的,因此您可能需要将其包装到Promise中:

callback