Vue.createApp不工作,但正在使用新的Vue()方法

我收到此错误tesyya.js:16 Uncaught TypeError: Vue.createApp is not a function mycode如下所示:

const app = Vue.createApp({
  data() {
    return {
      count: 4
    }
  }
})

const vm = app.mount('#app')

console.log(vm.count)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>My GK</title>
</head>

<body>
  <div class="app">
    <h1>this might be challenging for you</h1>
    <ul id="addhere">
      <li v-for="goal in goals">{{goal}}</li>
    </ul>
    <input type="text" name="text" id="addthis" v-model="enteredval" />
    <input type="button" value="ADD" id="add" v-on:click="add()" />
  </div>
  <script src="https://unpkg.com/vue"></script>
  <script src="tesyya.js"></script>
</body>

</html>

请允许我弄错,我是初学者


解决方案

createApp方法适用于VUE 3,错误指示您正在使用VUE 2。以下是具有正确语法的适用于VUE 2和VUE 3的等效示例应用程序。

版本2:

CDN:<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.min.js"></script>

new Vue({
  el: "#app",
  data() {
    return {
      someValue: 10
    }
  },
  computed: {
    someComputed() {
      return this.someValue * 10;
    }
  }
});
<div id="app">
  Some value: {{ someValue }} <br />
  Some computed value: {{ someComputed }}
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.14/vue.min.js"></script>

版本3:

CDN:<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.30/vue.global.min.js"></script>

const { createApp, ref, computed } = Vue;
const app = createApp({
  setup() {
    const someValue = ref(10);
    const someComputed = computed(() => someValue.value * 10);
    return {
      someValue,
      someComputed
    }
  }
});
app.mount("#app");
<div id="app">
  Some value: {{ someValue }} <br />
  Some computed value: {{ someComputed }}
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.30/vue.global.min.js"></script>

相关文章