Vue.js cannot add style from method
I'd like to add some styles into html element from methods:
<div class="add-profile-img" v-bind:style="getBackgroundImg()">
The method is:
getBackgroundImg: function() {
return {
width: 180px;
height: 180px;
background-color: 'yellow';
background-image:url(this.BASE_URL +'/uploads/noimg.gif');
}
},
However, I get
Syntax Error: Identifier directly after number (79:13)
77 | getBackgroundImg: function() {
78 | return {
> 79 | width: 180px;
| ^
How can I fix it?
javascript vue.js
add a comment |
I'd like to add some styles into html element from methods:
<div class="add-profile-img" v-bind:style="getBackgroundImg()">
The method is:
getBackgroundImg: function() {
return {
width: 180px;
height: 180px;
background-color: 'yellow';
background-image:url(this.BASE_URL +'/uploads/noimg.gif');
}
},
However, I get
Syntax Error: Identifier directly after number (79:13)
77 | getBackgroundImg: function() {
78 | return {
> 79 | width: 180px;
| ^
How can I fix it?
javascript vue.js
add a comment |
I'd like to add some styles into html element from methods:
<div class="add-profile-img" v-bind:style="getBackgroundImg()">
The method is:
getBackgroundImg: function() {
return {
width: 180px;
height: 180px;
background-color: 'yellow';
background-image:url(this.BASE_URL +'/uploads/noimg.gif');
}
},
However, I get
Syntax Error: Identifier directly after number (79:13)
77 | getBackgroundImg: function() {
78 | return {
> 79 | width: 180px;
| ^
How can I fix it?
javascript vue.js
I'd like to add some styles into html element from methods:
<div class="add-profile-img" v-bind:style="getBackgroundImg()">
The method is:
getBackgroundImg: function() {
return {
width: 180px;
height: 180px;
background-color: 'yellow';
background-image:url(this.BASE_URL +'/uploads/noimg.gif');
}
},
However, I get
Syntax Error: Identifier directly after number (79:13)
77 | getBackgroundImg: function() {
78 | return {
> 79 | width: 180px;
| ^
How can I fix it?
javascript vue.js
javascript vue.js
asked Nov 13 '18 at 19:00
BabrBabr
31019
31019
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Dimension in pixels need to be in string format so the function return a valid javascript object:
return {
width: '180px',
height: '180px',
'background-color': 'yellow',
'background-image': `url(${this.BASE_URL}/uploads/noimg.gif)`
}
I still get:Unexpected token, expected "," (79:17)
.
– Babr
Nov 13 '18 at 19:04
change semi colon to,
. See the update
– Psidom
Nov 13 '18 at 19:05
Well, now I getUnexpected token, expected "," (81:21)
pointing atbackground-image
's dash.
– Babr
Nov 13 '18 at 19:07
bothbackground-color
andbackground-image
should be quoted. Javascript variable name can't contain dash -
– Psidom
Nov 13 '18 at 19:10
1
I'm sorry I had an extra backtick hanging around. Your answer is correct. Thanks dude!
– Babr
Nov 13 '18 at 19:22
|
show 8 more comments
can I ask why would you want to do that? As far as I know if you bind a style, just create the object in the data object and do not forget to use the style sintax adapted for javascript. (Camelcase)
data(){
return{
yourStyleVariable: {
backgroundColor: 'red'
}
}
}
I want to usethis.BASE_URL
without hard coding its value. AFAIK I can not do that in<style>
part of vue component. Hence the question.
– Babr
Nov 13 '18 at 19:19
why not using a computed property?
– nedsilon
Nov 13 '18 at 19:47
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287839%2fvue-js-cannot-add-style-from-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Dimension in pixels need to be in string format so the function return a valid javascript object:
return {
width: '180px',
height: '180px',
'background-color': 'yellow',
'background-image': `url(${this.BASE_URL}/uploads/noimg.gif)`
}
I still get:Unexpected token, expected "," (79:17)
.
– Babr
Nov 13 '18 at 19:04
change semi colon to,
. See the update
– Psidom
Nov 13 '18 at 19:05
Well, now I getUnexpected token, expected "," (81:21)
pointing atbackground-image
's dash.
– Babr
Nov 13 '18 at 19:07
bothbackground-color
andbackground-image
should be quoted. Javascript variable name can't contain dash -
– Psidom
Nov 13 '18 at 19:10
1
I'm sorry I had an extra backtick hanging around. Your answer is correct. Thanks dude!
– Babr
Nov 13 '18 at 19:22
|
show 8 more comments
Dimension in pixels need to be in string format so the function return a valid javascript object:
return {
width: '180px',
height: '180px',
'background-color': 'yellow',
'background-image': `url(${this.BASE_URL}/uploads/noimg.gif)`
}
I still get:Unexpected token, expected "," (79:17)
.
– Babr
Nov 13 '18 at 19:04
change semi colon to,
. See the update
– Psidom
Nov 13 '18 at 19:05
Well, now I getUnexpected token, expected "," (81:21)
pointing atbackground-image
's dash.
– Babr
Nov 13 '18 at 19:07
bothbackground-color
andbackground-image
should be quoted. Javascript variable name can't contain dash -
– Psidom
Nov 13 '18 at 19:10
1
I'm sorry I had an extra backtick hanging around. Your answer is correct. Thanks dude!
– Babr
Nov 13 '18 at 19:22
|
show 8 more comments
Dimension in pixels need to be in string format so the function return a valid javascript object:
return {
width: '180px',
height: '180px',
'background-color': 'yellow',
'background-image': `url(${this.BASE_URL}/uploads/noimg.gif)`
}
Dimension in pixels need to be in string format so the function return a valid javascript object:
return {
width: '180px',
height: '180px',
'background-color': 'yellow',
'background-image': `url(${this.BASE_URL}/uploads/noimg.gif)`
}
edited Nov 13 '18 at 19:09
answered Nov 13 '18 at 19:02
PsidomPsidom
123k1283126
123k1283126
I still get:Unexpected token, expected "," (79:17)
.
– Babr
Nov 13 '18 at 19:04
change semi colon to,
. See the update
– Psidom
Nov 13 '18 at 19:05
Well, now I getUnexpected token, expected "," (81:21)
pointing atbackground-image
's dash.
– Babr
Nov 13 '18 at 19:07
bothbackground-color
andbackground-image
should be quoted. Javascript variable name can't contain dash -
– Psidom
Nov 13 '18 at 19:10
1
I'm sorry I had an extra backtick hanging around. Your answer is correct. Thanks dude!
– Babr
Nov 13 '18 at 19:22
|
show 8 more comments
I still get:Unexpected token, expected "," (79:17)
.
– Babr
Nov 13 '18 at 19:04
change semi colon to,
. See the update
– Psidom
Nov 13 '18 at 19:05
Well, now I getUnexpected token, expected "," (81:21)
pointing atbackground-image
's dash.
– Babr
Nov 13 '18 at 19:07
bothbackground-color
andbackground-image
should be quoted. Javascript variable name can't contain dash -
– Psidom
Nov 13 '18 at 19:10
1
I'm sorry I had an extra backtick hanging around. Your answer is correct. Thanks dude!
– Babr
Nov 13 '18 at 19:22
I still get:
Unexpected token, expected "," (79:17)
.– Babr
Nov 13 '18 at 19:04
I still get:
Unexpected token, expected "," (79:17)
.– Babr
Nov 13 '18 at 19:04
change semi colon to
,
. See the update– Psidom
Nov 13 '18 at 19:05
change semi colon to
,
. See the update– Psidom
Nov 13 '18 at 19:05
Well, now I get
Unexpected token, expected "," (81:21)
pointing at background-image
's dash.– Babr
Nov 13 '18 at 19:07
Well, now I get
Unexpected token, expected "," (81:21)
pointing at background-image
's dash.– Babr
Nov 13 '18 at 19:07
both
background-color
and background-image
should be quoted. Javascript variable name can't contain dash -– Psidom
Nov 13 '18 at 19:10
both
background-color
and background-image
should be quoted. Javascript variable name can't contain dash -– Psidom
Nov 13 '18 at 19:10
1
1
I'm sorry I had an extra backtick hanging around. Your answer is correct. Thanks dude!
– Babr
Nov 13 '18 at 19:22
I'm sorry I had an extra backtick hanging around. Your answer is correct. Thanks dude!
– Babr
Nov 13 '18 at 19:22
|
show 8 more comments
can I ask why would you want to do that? As far as I know if you bind a style, just create the object in the data object and do not forget to use the style sintax adapted for javascript. (Camelcase)
data(){
return{
yourStyleVariable: {
backgroundColor: 'red'
}
}
}
I want to usethis.BASE_URL
without hard coding its value. AFAIK I can not do that in<style>
part of vue component. Hence the question.
– Babr
Nov 13 '18 at 19:19
why not using a computed property?
– nedsilon
Nov 13 '18 at 19:47
add a comment |
can I ask why would you want to do that? As far as I know if you bind a style, just create the object in the data object and do not forget to use the style sintax adapted for javascript. (Camelcase)
data(){
return{
yourStyleVariable: {
backgroundColor: 'red'
}
}
}
I want to usethis.BASE_URL
without hard coding its value. AFAIK I can not do that in<style>
part of vue component. Hence the question.
– Babr
Nov 13 '18 at 19:19
why not using a computed property?
– nedsilon
Nov 13 '18 at 19:47
add a comment |
can I ask why would you want to do that? As far as I know if you bind a style, just create the object in the data object and do not forget to use the style sintax adapted for javascript. (Camelcase)
data(){
return{
yourStyleVariable: {
backgroundColor: 'red'
}
}
}
can I ask why would you want to do that? As far as I know if you bind a style, just create the object in the data object and do not forget to use the style sintax adapted for javascript. (Camelcase)
data(){
return{
yourStyleVariable: {
backgroundColor: 'red'
}
}
}
answered Nov 13 '18 at 19:16
nedsilonnedsilon
212
212
I want to usethis.BASE_URL
without hard coding its value. AFAIK I can not do that in<style>
part of vue component. Hence the question.
– Babr
Nov 13 '18 at 19:19
why not using a computed property?
– nedsilon
Nov 13 '18 at 19:47
add a comment |
I want to usethis.BASE_URL
without hard coding its value. AFAIK I can not do that in<style>
part of vue component. Hence the question.
– Babr
Nov 13 '18 at 19:19
why not using a computed property?
– nedsilon
Nov 13 '18 at 19:47
I want to use
this.BASE_URL
without hard coding its value. AFAIK I can not do that in <style>
part of vue component. Hence the question.– Babr
Nov 13 '18 at 19:19
I want to use
this.BASE_URL
without hard coding its value. AFAIK I can not do that in <style>
part of vue component. Hence the question.– Babr
Nov 13 '18 at 19:19
why not using a computed property?
– nedsilon
Nov 13 '18 at 19:47
why not using a computed property?
– nedsilon
Nov 13 '18 at 19:47
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287839%2fvue-js-cannot-add-style-from-method%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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