Reputation: 3
I am trying to print dynamic message using webpack 3 and ES6 using babel and hotmodulereplacement()
in webpack but, I cannot print messages. Below is the code.
cannot print:
var newMessage = () => ('<p>${messages.hi} ${messages.event}</p>');
messages.js file
module.exports = {
hi: 'Hello there content changed',
event: 'this is new webpack'
};
index.js file
var messages = require('./messages');
var newMessage = () => ('<p>${messages.hi} ${messages.event}</p>');
var app = document.getElementById('app');
app.innerHTML = newMessage();
if(module.hot){
module.hot.accept();
}
index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app"></div>
<script src="dist/bundle.js"></script>
</body>
</html>
Output on webpage:
${messages.hi} ${messages.event}
Upvotes: 0
Views: 37
Reputation: 291
You need to use backticks ( ` ) instead of quotes (').
`<p>${messages.hi} ${messages.event}</p>`
Upvotes: 1
Reputation: 3268
I suspect you want to use the new template literals: for that you have to enclose the string in backticks
`<p>${messages.hi} ${messages.event}</p>`
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Upvotes: 2