【问题标题】:How I know if the Vue app is mounted from javascript?我如何知道 Vue 应用程序是否是从 javascript 安装的?
【发布时间】:2021-02-06 23:17:39
【问题描述】:

我有这段代码来运行我的 Vue 应用程序:

if (document.getElementById('app') != null) {
  new Vue({
      router,
      store,
      data () {
          return {
              errors: [],
              django_context: CONTEXT
          }
      },
      created() {
          this.$store.dispatch('products/getProducts');
      },
      render: h => h(App),
  }).$mount("#app");
}

我需要知道应用程序是否安装了原版 javascript。类型

【问题讨论】:

  • 你不能设置一个全局标志,在安装应用程序后设置为 true 吗? window.mounted = true.
  • 它已安装如果你有一个带有id="app"的元素,否则它不是

标签: javascript vue.js vuejs2 vuex vue-router


【解决方案1】:

您可以检查您的挂载点是否设置了__vue__ 属性。

const mountPointSelector = "#app";
// [...]
const mountPoint = document.querySelector(mountPointSelector);
if (mountPoint) {
    return mountPoint.__vue__;
}

【讨论】:

    【解决方案2】:

    Vue 具有 mounted 生命周期。所以在created之后添加

      mounted() {
          console.log("Mounted!");
      },
    

    您可以阅读 herehere 以详细了解 Vue 的生命周期

    【讨论】:

    • 我需要了解 Vue 应用程序之外的信息。例如来自 jquery。
    • 这不是问题的答案。
    猜你喜欢
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多