In Vue.js how can I detect if code in created() hook runs in the browser? [duplicate]
This question already has an answer here:
Environment detection: node.js or browser
4 answers
I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.
I've set the process env in the Webpack config like
process.env.VUE_ENV === 'server'
which works. But for various reasons, I need a detection independent of the built environment.
I'd like to check for the browser/node in the created() hook.
How would I do that?
javascript vue.js
marked as duplicate by thanksd
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Environment detection: node.js or browser
4 answers
I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.
I've set the process env in the Webpack config like
process.env.VUE_ENV === 'server'
which works. But for various reasons, I need a detection independent of the built environment.
I'd like to check for the browser/node in the created() hook.
How would I do that?
javascript vue.js
marked as duplicate by thanksd
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You can try to access a browser specific object (like navigator object) within yourcreated()hook - and catch the error it throws when trying to access this object on nodejs fails
– Mortz
Nov 21 '18 at 10:37
stackoverflow.com/questions/4224606/…
– ssc-hrep3
Nov 21 '18 at 10:38
add a comment |
This question already has an answer here:
Environment detection: node.js or browser
4 answers
I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.
I've set the process env in the Webpack config like
process.env.VUE_ENV === 'server'
which works. But for various reasons, I need a detection independent of the built environment.
I'd like to check for the browser/node in the created() hook.
How would I do that?
javascript vue.js
This question already has an answer here:
Environment detection: node.js or browser
4 answers
I have a Vue SSR-application and for some components, I need to know whether they run on Node during server-side rendering or whether they run on the browser.
I've set the process env in the Webpack config like
process.env.VUE_ENV === 'server'
which works. But for various reasons, I need a detection independent of the built environment.
I'd like to check for the browser/node in the created() hook.
How would I do that?
This question already has an answer here:
Environment detection: node.js or browser
4 answers
javascript vue.js
javascript vue.js
edited Nov 21 '18 at 10:23
Yashwardhan Pauranik
2,03111529
2,03111529
asked Nov 21 '18 at 10:14
LongHikeLongHike
7651123
7651123
marked as duplicate by thanksd
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by thanksd
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 '18 at 15:48
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
You can try to access a browser specific object (like navigator object) within yourcreated()hook - and catch the error it throws when trying to access this object on nodejs fails
– Mortz
Nov 21 '18 at 10:37
stackoverflow.com/questions/4224606/…
– ssc-hrep3
Nov 21 '18 at 10:38
add a comment |
1
You can try to access a browser specific object (like navigator object) within yourcreated()hook - and catch the error it throws when trying to access this object on nodejs fails
– Mortz
Nov 21 '18 at 10:37
stackoverflow.com/questions/4224606/…
– ssc-hrep3
Nov 21 '18 at 10:38
1
1
You can try to access a browser specific object (like navigator object) within your
created() hook - and catch the error it throws when trying to access this object on nodejs fails– Mortz
Nov 21 '18 at 10:37
You can try to access a browser specific object (like navigator object) within your
created() hook - and catch the error it throws when trying to access this object on nodejs fails– Mortz
Nov 21 '18 at 10:37
stackoverflow.com/questions/4224606/…
– ssc-hrep3
Nov 21 '18 at 10:38
stackoverflow.com/questions/4224606/…
– ssc-hrep3
Nov 21 '18 at 10:38
add a comment |
1 Answer
1
active
oldest
votes
I took the following line -verbatim- from the Vue.js source code ..
const inBrowser = typeof window !== 'undefined';
You can use it to verify if your code is running in the browser.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I took the following line -verbatim- from the Vue.js source code ..
const inBrowser = typeof window !== 'undefined';
You can use it to verify if your code is running in the browser.
add a comment |
I took the following line -verbatim- from the Vue.js source code ..
const inBrowser = typeof window !== 'undefined';
You can use it to verify if your code is running in the browser.
add a comment |
I took the following line -verbatim- from the Vue.js source code ..
const inBrowser = typeof window !== 'undefined';
You can use it to verify if your code is running in the browser.
I took the following line -verbatim- from the Vue.js source code ..
const inBrowser = typeof window !== 'undefined';
You can use it to verify if your code is running in the browser.
answered Nov 21 '18 at 12:06
Husam IbrahimHusam Ibrahim
3,323417
3,323417
add a comment |
add a comment |
1
You can try to access a browser specific object (like navigator object) within your
created()hook - and catch the error it throws when trying to access this object on nodejs fails– Mortz
Nov 21 '18 at 10:37
stackoverflow.com/questions/4224606/…
– ssc-hrep3
Nov 21 '18 at 10:38