javascript - 为 mvc5 项目导出 es6 模块
问题描述
我正在尝试为 mvc5 项目(不是核心)设置 webpack。一切正常。除了在 cshtml 页面的脚本中引用模块。例如在我的布局页面中。
<script src="~/Scripts/build/bundle.js"></script>
在 main.js 中
import Person from './person';
var person = new Person("David", 20);
person.speak();
和 person.js
export default class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
speak() {
console.log(`Hi I'm ${this.name} and ${this.age} years old and I am awesome`);
}
}
当我运行应用程序时,这很好用,我在控制台中看到了输出。
然而,试图引用 index.cshtml 中的人给我带来了问题。
我将 main.js 更改为。
module.exports = {
Person: require('./person'),
};
在我看来,我试图引用人
在 index.cshtml 中(正在使用布局页面)
<script>
var person = new Person("David", 20);
person.speak();
</script>
我收到
未捕获的 ReferenceError:Person 未在索引处定义:64
解决方案
推荐阅读
- ruby - 将块作为变量传递失败“ArgumentError:缺少块”
- matlab - 基本 Matlab for 循环
- events - 如何在实体 AFRAME 上进行焦点事件?
- spring-boot - 在运行时根据文件扩展名设置目标位置
- c# - 如何在 SQL Server 通信中禁用 C# 中的 NetBios
- sql-server - 如何修复 System.Data.SqlClient.SqlException:“用户“xx\xxxx$”登录失败”
- c - 结构填充的规则是什么
- html - 显示 HTML5 进度条的内部文本?没有实施的技术原因?
- asp.net-mvc - 如何使用实体框架连接两个表
- r - 有没有办法在换出一个变量时循环 R 分类模型?