簡體   English   中英

如何在組件 vue 中使用 file.js

[英]How can I use a file.js in a component vue

我嘗試在我的應用程序 (InputSwap.vue) 的其中一個組件中使用 prueba.js,其中有一個按鈕 ('console.log')。 我想通過那個按鈕來使用這個文件,但應用程序向我顯示了這個錯誤:

在此處輸入圖片說明

prueba.js 讓我通過單擊按鈕在控制台中查看數據。 數據是用 window.localStorage 保存的:

window.localStorage.setItem('data_input', JSON.stringify(data));

我錯在哪里?


prueba.js :

export default {
   infoPrueba() {
        var data = (JSON.parse(window.localStorage.getItem('data_input')))
        console.log(data)
    }
}

InputSwap.vue:

<template>
    <div class="card-action">
      <Button v-on:click="prueba()"
        v-bind:style="{'margin-left' : '5px', background : '#52368c'}"
        btext="Console.log" icon="code"
      />
    </div>
</template>
<script>
import Button from './Button'
import * as prueba from './prueba.js' // I have prueba.js in components folder
export default {
  name: 'InputSwap',
  components: {Button},
  methods: {
    prueba: async function () {
      prueba.infoPrueba()
    },
  },
}
</script>

感謝 x-rw,我解決了這個問題:

我將 import * as prueba from './prueba.js' 改為 import {infoPueba} from './prueba.js'

我在 prueba.js 中寫了這段代碼:

export const infoPrueba = () => {
  var data = (JSON.parse(window.localStorage.getItem('data_input')))
  console.log(data)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM