pass image path to child from parent in vue.js with vue-cli











up vote
1
down vote

favorite
1












[DONE] A non-comment is a loaded



I just start using vue.js with "vue-cli" just now.



but, now I faced a problem.



purpose : I want the parent component to pass the image path to the child component as a string, and the child to load the image using it.



However, in the process of bundling the webpack, It can not call it unless it have to do hash processing like other images, only first image components work.
Do I need to configure vue.config.js differently?
Or is it possible to change the way an image is called in a vue?



thanks.



<template lang="pug">
.container
img(src="../assets/image/logo.png")
//- img(style='{ "width" : "200px" , "height" : "200px" }' :style="{backgroundImage : logoSrcUrl }")
//- img(:src="logoSrc")
//- img(:src="logoSrcUrl")
//- img(:src="logoSrcRequire")
img(:src="getImg(this.$props.imgSrc)")
img(:src="stringRequire")
//- img(:src="logoSrcData")
//- img(:src="logoSrcDataUrl")
</template>

<script>
export default {
props : {
"imageDir" : String,
"imgSrc" : String,
},
data(){
return {
logoSrcData : '',
}
},
computed : {
logoSrc(){
return this.$props.imageDir + this.$props.imgSrc
},
logoSrcRequire(){
return require(this.logoSrcData)
},
stringRequire(){
return require("../assets/image/logo.png")
},
logoSrcUrl(){
return `url(${this.$props.imageDir}${this.$props.imgSrc})`
},
logoSrcDataUrl(){
return `url(${this.logoSrcData})`
},
},
methods : {
getImg(fileName){
return require('../assets/image/' + fileName)
}
},
created() {
console.log(this.$props.imageDir) // ../assets/image/
console.log(this.$props.imgSrc) // logo.png
this.$data.logoSrcData = this.$props.imageDir + this.$props.imgSrc
console.log(this.logoSrcData) // ../assets/image/logo.png
},
}
</script>

<style scoped>

</style>


In addition, when bundling with WebPack, can I implement additional work to change the hash value only for the changed image? Or does WebPack need a pre-bundler because it does not refer to previous bundling files?










share|improve this question




















  • 1




    the question is, do you want webpack to bundle your images? or do you want relative paths towards your public assets? cli.vuejs.org/guide/…
    – cal_br_mar
    Nov 8 at 2:36










  • the purpose is to load the image with a string variable. ps. I am sorry that my English skills were not good and I did not write the message clearly.
    – ParkDyel
    Nov 8 at 3:15












  • The documentation around this isn't great but you need to require the image path in order for Webpack to bundle it.
    – Phil
    Nov 8 at 4:29










  • Possible duplicate of Vue.js dynamic images not working
    – Phil
    Nov 8 at 4:32






  • 1




    Thank you. I failed this document with computed for the last time, but this time I have access with methods. thanks!
    – ParkDyel
    Nov 8 at 5:31















up vote
1
down vote

favorite
1












[DONE] A non-comment is a loaded



I just start using vue.js with "vue-cli" just now.



but, now I faced a problem.



purpose : I want the parent component to pass the image path to the child component as a string, and the child to load the image using it.



However, in the process of bundling the webpack, It can not call it unless it have to do hash processing like other images, only first image components work.
Do I need to configure vue.config.js differently?
Or is it possible to change the way an image is called in a vue?



thanks.



<template lang="pug">
.container
img(src="../assets/image/logo.png")
//- img(style='{ "width" : "200px" , "height" : "200px" }' :style="{backgroundImage : logoSrcUrl }")
//- img(:src="logoSrc")
//- img(:src="logoSrcUrl")
//- img(:src="logoSrcRequire")
img(:src="getImg(this.$props.imgSrc)")
img(:src="stringRequire")
//- img(:src="logoSrcData")
//- img(:src="logoSrcDataUrl")
</template>

<script>
export default {
props : {
"imageDir" : String,
"imgSrc" : String,
},
data(){
return {
logoSrcData : '',
}
},
computed : {
logoSrc(){
return this.$props.imageDir + this.$props.imgSrc
},
logoSrcRequire(){
return require(this.logoSrcData)
},
stringRequire(){
return require("../assets/image/logo.png")
},
logoSrcUrl(){
return `url(${this.$props.imageDir}${this.$props.imgSrc})`
},
logoSrcDataUrl(){
return `url(${this.logoSrcData})`
},
},
methods : {
getImg(fileName){
return require('../assets/image/' + fileName)
}
},
created() {
console.log(this.$props.imageDir) // ../assets/image/
console.log(this.$props.imgSrc) // logo.png
this.$data.logoSrcData = this.$props.imageDir + this.$props.imgSrc
console.log(this.logoSrcData) // ../assets/image/logo.png
},
}
</script>

<style scoped>

</style>


In addition, when bundling with WebPack, can I implement additional work to change the hash value only for the changed image? Or does WebPack need a pre-bundler because it does not refer to previous bundling files?










share|improve this question




















  • 1




    the question is, do you want webpack to bundle your images? or do you want relative paths towards your public assets? cli.vuejs.org/guide/…
    – cal_br_mar
    Nov 8 at 2:36










  • the purpose is to load the image with a string variable. ps. I am sorry that my English skills were not good and I did not write the message clearly.
    – ParkDyel
    Nov 8 at 3:15












  • The documentation around this isn't great but you need to require the image path in order for Webpack to bundle it.
    – Phil
    Nov 8 at 4:29










  • Possible duplicate of Vue.js dynamic images not working
    – Phil
    Nov 8 at 4:32






  • 1




    Thank you. I failed this document with computed for the last time, but this time I have access with methods. thanks!
    – ParkDyel
    Nov 8 at 5:31













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





[DONE] A non-comment is a loaded



I just start using vue.js with "vue-cli" just now.



but, now I faced a problem.



purpose : I want the parent component to pass the image path to the child component as a string, and the child to load the image using it.



However, in the process of bundling the webpack, It can not call it unless it have to do hash processing like other images, only first image components work.
Do I need to configure vue.config.js differently?
Or is it possible to change the way an image is called in a vue?



thanks.



<template lang="pug">
.container
img(src="../assets/image/logo.png")
//- img(style='{ "width" : "200px" , "height" : "200px" }' :style="{backgroundImage : logoSrcUrl }")
//- img(:src="logoSrc")
//- img(:src="logoSrcUrl")
//- img(:src="logoSrcRequire")
img(:src="getImg(this.$props.imgSrc)")
img(:src="stringRequire")
//- img(:src="logoSrcData")
//- img(:src="logoSrcDataUrl")
</template>

<script>
export default {
props : {
"imageDir" : String,
"imgSrc" : String,
},
data(){
return {
logoSrcData : '',
}
},
computed : {
logoSrc(){
return this.$props.imageDir + this.$props.imgSrc
},
logoSrcRequire(){
return require(this.logoSrcData)
},
stringRequire(){
return require("../assets/image/logo.png")
},
logoSrcUrl(){
return `url(${this.$props.imageDir}${this.$props.imgSrc})`
},
logoSrcDataUrl(){
return `url(${this.logoSrcData})`
},
},
methods : {
getImg(fileName){
return require('../assets/image/' + fileName)
}
},
created() {
console.log(this.$props.imageDir) // ../assets/image/
console.log(this.$props.imgSrc) // logo.png
this.$data.logoSrcData = this.$props.imageDir + this.$props.imgSrc
console.log(this.logoSrcData) // ../assets/image/logo.png
},
}
</script>

<style scoped>

</style>


In addition, when bundling with WebPack, can I implement additional work to change the hash value only for the changed image? Or does WebPack need a pre-bundler because it does not refer to previous bundling files?










share|improve this question















[DONE] A non-comment is a loaded



I just start using vue.js with "vue-cli" just now.



but, now I faced a problem.



purpose : I want the parent component to pass the image path to the child component as a string, and the child to load the image using it.



However, in the process of bundling the webpack, It can not call it unless it have to do hash processing like other images, only first image components work.
Do I need to configure vue.config.js differently?
Or is it possible to change the way an image is called in a vue?



thanks.



<template lang="pug">
.container
img(src="../assets/image/logo.png")
//- img(style='{ "width" : "200px" , "height" : "200px" }' :style="{backgroundImage : logoSrcUrl }")
//- img(:src="logoSrc")
//- img(:src="logoSrcUrl")
//- img(:src="logoSrcRequire")
img(:src="getImg(this.$props.imgSrc)")
img(:src="stringRequire")
//- img(:src="logoSrcData")
//- img(:src="logoSrcDataUrl")
</template>

<script>
export default {
props : {
"imageDir" : String,
"imgSrc" : String,
},
data(){
return {
logoSrcData : '',
}
},
computed : {
logoSrc(){
return this.$props.imageDir + this.$props.imgSrc
},
logoSrcRequire(){
return require(this.logoSrcData)
},
stringRequire(){
return require("../assets/image/logo.png")
},
logoSrcUrl(){
return `url(${this.$props.imageDir}${this.$props.imgSrc})`
},
logoSrcDataUrl(){
return `url(${this.logoSrcData})`
},
},
methods : {
getImg(fileName){
return require('../assets/image/' + fileName)
}
},
created() {
console.log(this.$props.imageDir) // ../assets/image/
console.log(this.$props.imgSrc) // logo.png
this.$data.logoSrcData = this.$props.imageDir + this.$props.imgSrc
console.log(this.logoSrcData) // ../assets/image/logo.png
},
}
</script>

<style scoped>

</style>


In addition, when bundling with WebPack, can I implement additional work to change the hash value only for the changed image? Or does WebPack need a pre-bundler because it does not refer to previous bundling files?







vue.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 5:29

























asked Nov 8 at 2:28









ParkDyel

3619




3619








  • 1




    the question is, do you want webpack to bundle your images? or do you want relative paths towards your public assets? cli.vuejs.org/guide/…
    – cal_br_mar
    Nov 8 at 2:36










  • the purpose is to load the image with a string variable. ps. I am sorry that my English skills were not good and I did not write the message clearly.
    – ParkDyel
    Nov 8 at 3:15












  • The documentation around this isn't great but you need to require the image path in order for Webpack to bundle it.
    – Phil
    Nov 8 at 4:29










  • Possible duplicate of Vue.js dynamic images not working
    – Phil
    Nov 8 at 4:32






  • 1




    Thank you. I failed this document with computed for the last time, but this time I have access with methods. thanks!
    – ParkDyel
    Nov 8 at 5:31














  • 1




    the question is, do you want webpack to bundle your images? or do you want relative paths towards your public assets? cli.vuejs.org/guide/…
    – cal_br_mar
    Nov 8 at 2:36










  • the purpose is to load the image with a string variable. ps. I am sorry that my English skills were not good and I did not write the message clearly.
    – ParkDyel
    Nov 8 at 3:15












  • The documentation around this isn't great but you need to require the image path in order for Webpack to bundle it.
    – Phil
    Nov 8 at 4:29










  • Possible duplicate of Vue.js dynamic images not working
    – Phil
    Nov 8 at 4:32






  • 1




    Thank you. I failed this document with computed for the last time, but this time I have access with methods. thanks!
    – ParkDyel
    Nov 8 at 5:31








1




1




the question is, do you want webpack to bundle your images? or do you want relative paths towards your public assets? cli.vuejs.org/guide/…
– cal_br_mar
Nov 8 at 2:36




the question is, do you want webpack to bundle your images? or do you want relative paths towards your public assets? cli.vuejs.org/guide/…
– cal_br_mar
Nov 8 at 2:36












the purpose is to load the image with a string variable. ps. I am sorry that my English skills were not good and I did not write the message clearly.
– ParkDyel
Nov 8 at 3:15






the purpose is to load the image with a string variable. ps. I am sorry that my English skills were not good and I did not write the message clearly.
– ParkDyel
Nov 8 at 3:15














The documentation around this isn't great but you need to require the image path in order for Webpack to bundle it.
– Phil
Nov 8 at 4:29




The documentation around this isn't great but you need to require the image path in order for Webpack to bundle it.
– Phil
Nov 8 at 4:29












Possible duplicate of Vue.js dynamic images not working
– Phil
Nov 8 at 4:32




Possible duplicate of Vue.js dynamic images not working
– Phil
Nov 8 at 4:32




1




1




Thank you. I failed this document with computed for the last time, but this time I have access with methods. thanks!
– ParkDyel
Nov 8 at 5:31




Thank you. I failed this document with computed for the last time, but this time I have access with methods. thanks!
– ParkDyel
Nov 8 at 5:31

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200703%2fpass-image-path-to-child-from-parent-in-vue-js-with-vue-cli%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200703%2fpass-image-path-to-child-from-parent-in-vue-js-with-vue-cli%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud