javascript - Uncaught TypeError : dataOptions. call is not a function when load entry in vue 3

标签 javascript vuejs3

我将 vue 升级到 3.x "vue": "^3.2.28",然后像这样调整代码初始应用程序:

import Vue from 'vue';
import template from './tpl.html';
import chromeCall from 'chrome-call';
import getOptions from '../public/default-options';
import {getTabLocation,isHostEnabled} from '../public/util';
import ST from './st';
import { createApp } from "vue";

export const appOptions = {
  el : 'app' ,
  template ,
  data : {
    _host : null ,
    canInject : false ,
    enabled : false
  } ,
  methods : {
    async switchEnable() {
      const {_host} = this.$data ,
        enabled = this.enabled = !this.enabled ,
        {excludeDomains} = await getOptions( 'excludeDomains' );

      if ( enabled ) {
        excludeDomains.splice( excludeDomains.indexOf( _host ) , 1 );
      } else {
        excludeDomains.push( _host );
      }
      return chromeCall( 'storage.local.set' , { excludeDomains } );
    }
  } ,
  components : {
    'st-box' : ST
  } ,
  async ready() {
    const locationObj = await getTabLocation();
    if ( locationObj ) {
      this.$data._host = locationObj.host;
      this.canInject = true;
      this.enabled = await isHostEnabled( locationObj );
    }
  }
};

/* istanbul ignore if */
if ( process.env.NODE_ENV !== 'testing' ) {
  window.onload = ()=> {
    setTimeout( ()=> {
      const app = createApp(appOptions);
      app.mount("app");
    }, 0 );
  };
}

我用 Vue 3 方式更改了应用程序的首字母。遗留初始是 new Vue(),新方法是 createApp。当我运行这段代码时,显示如下错误:

Uncaught TypeError: dataOptions.call is not a function
    at applyOptions (commons1.js:4175:34)
    at finishComponentSetup (commons1.js:8531:9)
    at setupStatefulComponent (commons1.js:8443:9)
    at setupComponent (commons1.js:8373:11)
    at mountComponent (commons1.js:6296:13)
    at processComponent (commons1.js:6271:17)
    at patch (commons1.js:5872:21)
    at render (commons1.js:7015:13)
    at mount (commons1.js:5261:25)
    at Object.app.mount (commons1.js:10834:23)

我错过了什么?我应该如何调整我的代码以使其正常工作?

最佳答案

最后我弄清楚 dataOptionsappOptions 中定义的数据,vue 3 无法理解,在旧版 vue 中,这样定义数据:

data() {
  return {
    foo: 1,
    bar: { name: "hi" }
  }
}

在vue 3中,我们应该这样定义:

setup() {
  const foo = ref(1);
  const bar = reactive({ name: "hi" });

  return { foo, bar }
}

更多信息在here .我这样更改了我的代码:

setup () {
    const host =null;
    const canInject =false;
    const enabled = false;
    return {host,canInject,enabled};
  } ,

关于javascript - Uncaught TypeError : dataOptions. call is not a function when load entry in vue 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70822554/

相关文章:

javascript - 如何在javascript中比较列表框值和表值?

javascript - 使用javascript在ibooks epub中查找当前查看页面的页码

javascript - 当我的所有 Ng-from 都有效时启用一键

javascript - Vue 3 Google 登录用户身份验证在控制台中显示弃用警告

vue.js - 单击事件后从可组合项启动 useStore()

javascript - 想要删除行 onclick

javascript - 将 Javascript 事件添加到 SVG <def> 元素而不是直接添加到子元素上?

typescript - 如何在 Vue3 中使用 TypeScript 为 ref 定义类型(绑定(bind)模板)?

vue.js - Vue 组合 API : Defining emits

vue.js - 在VueJS中使用v-for时如何向项目添加id