我如何将文件vuejs组件导入index.html?

问题描述 投票:0回答:2

我正在为VUEjs使用CDN,因为我不想使用Webpack或从服务器渲染。

index.html和test-component.vue在同一文件夹中

我的index.html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>VUE</title>

<!-- vuejs -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

</head>
<body>

<div id="app">

  <!--HERE I WANT TO USE MY COMPONENT-->
  <comp></comp> 
</div>

<script>

  //TRIED TO IMPORT MY COMPONENT
  import comp from './test-component.vue';

  const app = new Vue({
    el: '#app',
  });

</script>


</body>
</html>

我的test-component.vue文件:

<template>
  <h1>im a component</h1>
</template>

<script>
  console.log('testing component');
</script>
javascript html vue.js frontend cdn
2个回答
0
投票

显然浏览器本身不支持.vue文件

有两种可能的解决方案:


0
投票

这里举个例子

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>VUE</title>

<!-- vuejs -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>

</head>
<body>

<div id="app">

  <!--HERE I WANT TO USE MY COMPONENT-->
  <button-counter></button-counter>
</div>

<script>

  Vue.component('button-counter', {
  data: function () {
    return {
      count: 0
    }
  },
  template: '<button v-on:click="count++">You clicked me {{ count }} times.</button>'
})

  const app = new Vue({
    el: '#app',
  });

</script>


</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.