javascript - 如何在组件 vue 中使用 file.js
问题描述
我尝试在我的应用程序(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)
}
}
输入交换.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)
}
推荐阅读
- javascript - 使用 socket.io-client 连接到 redis 服务器,而无需节点进行本机反应
- reactjs - 无法将输入图片数据保存在状态变量 React.js 中
- mysql - WAMP:使用 .MYD、.frm、.MYI、db.opt 恢复数据库
- r - 如何找到与功能相关的包
- python - 创建一个 api 来公开使用 fast.ai CNN 创建的 dl 模型
- python - 质心 Voronoi 镶嵌
- json - Discord.py 如何使用 json 来存储用户 ID 并且仅存储用户 ID
- authentication - 是否应该在注销时删除刷新令牌?
- sql - 在 SQL 中。为什么这个带有通配符的“Like”语句不起作用?
- r - R grep 最终的外部括号集