\r\n```\r\n上のdoSomething(data)内のdata部分だけを引数から代入したいのですがSyntaxErrorで読み込めません。\r\nどのように書けばrequire('')の中でもうまく書けるでしょうか。\r\n\r\n#追記\r\nself.imageSrc = \"require('~/assets/img/\"+data.data[0].brand+\".svg')\";\r\nとした時のエラーです\r\n```\r\nGET http://localhost:3000/require('~/assets/img/sample.svg') 404 (Not Found)\r\n```","answerCount":3,"upvoteCount":0,"datePublished":"2019-04-09T04:06:30.133Z","dateModified":"2019-04-09T04:35:04.351Z","acceptedAnswer":{"@type":"Answer","text":"```\r\n\\'\"~/assets/img/\" + data + \".svg\"\\'\r\n```\r\n\r\n```\r\n\\' // これ必要?\r\n```","dateModified":"2019-04-09T04:35:10.891Z","datePublished":"2019-04-09T04:35:10.891Z","upvoteCount":2,"url":"https://teratail.com/questions/183565#reply-272991"},"suggestedAnswer":[{"@type":"Answer","text":"```vue\r\nimageSrc: require(\"@/assets/img/default.svg\")\r\n```\r\n\r\nこれでどうでしょうか?","dateModified":"2019-04-09T04:19:50.703Z","datePublished":"2019-04-09T04:19:50.703Z","upvoteCount":1,"url":"https://teratail.com/questions/183565#reply-272987","comment":[{"@type":"Comment","text":"説明不足ですみません!imageSrcの方は読み込めてるっぽいんですが、\r\nself.imageSrc = require(\\\" \"~/assets/img/\" + data + \".svg\" \\\");\r\n部分がSyntaxErrorになってしまうんです。。","datePublished":"2019-04-09T04:30:07.132Z","dateModified":"2019-04-09T04:30:07.132Z"},{"@type":"Comment","text":"エラーの内容を全部示してもらえますか?\r\nあと、selfじゃなくて単純にthisでいいと思うのですが。","datePublished":"2019-04-09T04:32:58.704Z","dateModified":"2019-04-09T04:32:58.704Z"},{"@type":"Comment","text":"読み込めてないじゃないですか...","datePublished":"2019-04-09T04:40:52.057Z","dateModified":"2019-04-09T04:40:52.057Z"},{"@type":"Comment","text":"すみませんthisでも動きました。\r\n\r\nコンソールのエラー文こちらです。\r\n ERROR Failed to compile with 1 errors friendly-errors 13:32:14\r\n\r\nThis dependency was not found: friendly-errors 13:32:14\r\n friendly-errors 13:32:14\r\n* ~/assets/img/\"+data+\".svg in ./node_modules/babel-loader/lib??ref--2-0!./node_modules/vue-loader/lib??vue-loader-options!./pages/index/rental.vue?vue&type=script&lang=js&\r\n friendly-errors 13:32:14\r\nTo install it, you can run: npm install --save ~/assets/img/\"+data+\".svg","datePublished":"2019-04-09T04:45:06.955Z","dateModified":"2019-04-09T04:45:06.955Z"},{"@type":"Comment","text":"どこにsyntax errorと出ていますでしょうか?\r\nとにかくパスが間違っているんですよ。\r\n回答に書いてあること試しましたか?\r\nあと、ちゃんとファイル名とか拡張子まであっているかもう一度確認してください","datePublished":"2019-04-09T04:47:39.028Z","dateModified":"2019-04-09T04:47:39.028Z"},{"@type":"Comment","text":"self.imageSrc = require(\"~/assets/img/\" + data + \".svg\");\r\nにすると動きました!!\r\nエラーの読み方がおかしかったですね、、すみませんm(_ _)m","datePublished":"2019-04-09T04:55:44.584Z","dateModified":"2019-04-09T04:55:44.584Z"},{"@type":"Comment","text":"私も\r\nGET http://localhost:3000/require('~/assets/img/sample.svg') 404 (Not Found)\r\nのパスが訳わからないことになっていることに気付けず申し訳ないです。\r\n全く想定していないことをしていたのでビックリしました????","datePublished":"2019-04-09T04:59:52.212Z","dateModified":"2019-04-09T04:59:52.212Z"},{"@type":"Comment","text":"いえこんなに早く反応していただけてもうの凄いありがいです!!ありがとうございました!","datePublished":"2019-04-09T05:04:31.368Z","dateModified":"2019-04-09T05:04:31.368Z"}]},{"@type":"Answer","text":"そもそも`src`には**パス文字列を指定する**ものですので、`require`自体が必要ないのではないでしょうか。","dateModified":"2019-04-09T04:16:34.137Z","datePublished":"2019-04-09T04:16:34.137Z","upvoteCount":1,"url":"https://teratail.com/questions/183565#reply-272984","comment":[{"@type":"Comment","text":"変数をバインドする際はrequireしないとパスがおかしくなるはずです。","datePublished":"2019-04-09T04:18:31.858Z","dateModified":"2019-04-09T04:18:31.858Z"},{"@type":"Comment","text":"require自体はうまく動いてるんですが、メソッド内のdataを代入するために、require部分を文字列として囲もうとするとSyntaxErrorになっちゃいます。","datePublished":"2019-04-09T04:31:24.535Z","dateModified":"2019-04-09T04:31:24.535Z"}]}],"breadcrumb":{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://teratail.com","name":"トップ"}},{"@type":"ListItem","position":2,"item":{"@id":"https://teratail.com/tags/Vue.js","name":"Vue.jsに関する質問"}},{"@type":"ListItem","position":3,"item":{"@id":"https://teratail.com/questions/183565","name":"require内の一部分に変数を挿入できない"}}]}}}
nuxt.jsでのSPAを作っているんですが、imgタグのsrcをbindして動的に切り替えたいんですがrequire('~/assets/im/****.svg')内の「****」部分を変数にして切り替えたいんですが、読み込まれません。
vue.js
1 <template>
2 <div>
3 <img :src="imageSrc">
4 </div>
5 </template>
6
7 <script>
8 export default{
9 data(){
10 return{
11 imageSrc: require("~/assets/img/default.svg")
12 }
13 },
14 methods: {
15 doSomething(data){
16 var self = this;
17 self.imageSrc = require(\'"~/assets/img/" + data + ".svg"\');
18 }
19 }
20 }
21 </script>
上のdoSomething(data)内のdata部分だけを引数から代入したいのですがSyntaxErrorで読み込めません。
どのように書けばrequire('')の中でもうまく書けるでしょうか。
#追記
self.imageSrc = "require('~/assets/img/"+data.data[0].brand+".svg')";
とした時のエラーです
GET http://localhost:3000/require('~/assets/img/sample.svg') 404 (Not Found)
ベストアンサー
\'"~/assets/img/" + data + ".svg"\'
vue
1 imageSrc: require("@/assets/img/default.svg")
これでどうでしょうか?
そもそもsrcにはパス文字列を指定する ものですので、require自体が必要ないのではないでしょうか。
15分調べてもわからないことは teratailで質問しよう!
ただいまの回答率 85.29%
質問をまとめることで 思考を整理して素早く解決
テンプレート機能で 簡単に質問をまとめる
質問する
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/09 04:36 編集
2019/04/09 04:38
2019/04/09 04:42
2019/04/09 04:44
2019/04/09 04:48
2019/04/09 04:53