我在 mvc4 中使用 require js。
看法
<script data-main="/scripts/about" src="/scripts/require.js" ></script>
<article>
<p>
Use this area to provide additional information.
</p>
</article>
about.js
require.config({
paths: {
page1: '/Scripts/page1',
page2: '/Scripts/page2',
knockout : '/Scripts/knockout-2.2.0'
}
});
require(['page1', 'page2','knockout'], function (page1, page2) {
var car = new page1.Car();
car.speed(60);
var cycle = new page2.Cycle();
cycle.speed(20);
console.log(car.speed());
console.log(cycle.speed());
});
page1.js
define(function(){
function Car() {
var self = this;
self.speed = ko.observable();
}
});
第2页
define(function () {
function Cycle() {
var self = this;
self.speed = ko.observable();
}
});
在控制台中它说Car
未定义。
更新
define(['knockout'],function(ko){
function Car() {
var self = this;
self.speed = ko.observable();
return {
speed: self.speed() // But don't forget to declare it in the returned object!
};
}
});
我在 page1 和 page2 都这样做了
提前致谢