如何按路径加载特定文件?

时间:2019-09-18 11:14:59

标签: javascript vue.js

我有应用程序,我想添加按我的路径文件加载特定文件的按钮。 但是我没有成功获取特定文件。 我尝试以下代码:

按钮:

<b-button variant="outline-primary" @click="loadConfig(file)">Load default configuration</b-button>

file: new File([new Blob()], "253.01.00.00.SD.json", {
        type: "application/json"
      }```


2 个答案:

答案 0 :(得分:0)

您可以为此使用访存

var vm = new Vue({
  ....
  methods: {
    loadConfig(file) {
      fetch('253.01.00.00.SD.json') //or fetch(file) if you want to fetch the given file
        .then(function(response) {
          return response.json();
        })
        .then(function(myJson) {
          console.log(JSON.stringify(myJson));
        });
    }
 }
 ....

答案 1 :(得分:0)

我假设253.01.00.00.SD.json是您本地目录中的某个文件。

import json from './253.01.00.00.SD.json'

export default{
  data(){
    return{
      ourData: json
    }
  }
 }

在模板中,您可以执行以下操作:

<template>
  <div>
    <div v-for="data in ourData">{{data}}</div>
  </div>
</template>