下面列出了最常用的方法。如果哪个方法在下面没有列出,则说明那个方法用于高级用法。
m("div.class#id", {title: "title"}, ["children"])
var state = {
count: 0,
inc: function() {state.count++}
}
var Counter = {
view: function() {
return m("div", {onclick: state.inc}, state.count)
}
}
m.mount(document.body, Counter)
var Home = {
view: function() {
return "Welcome"
}
}
m.route(document.body, "/home", {
"/home": Home, // defines `http://localhost/#!/home`
})
m.route.set("/home")
var currentRoute = m.route.get()
在 m.route()
之前调用。
m.route.prefix("#!")
m("a[href='/Home']", {oncreate: m.route.link}, "Go to home page")
m.request({
method: "PUT",
url: "/api/v1/users/:id",
data: {id: 1, name: "test"}
})
.then(function(result) {
console.log(result)
})
m.jsonp({
url: "/api/v1/users/:id",
data: {id: 1},
callbackKey: "callback",
})
.then(function(result) {
console.log(result)
})
var object = m.parseQueryString("a=1&b=2")
// {a: "1", b: "2"}
var querystring = m.buildQueryString({a: "1", b: "2"})
// "a=1&b=2"
var state = {
value: "",
setValue: function(v) {state.value = v}
}
var Component = {
view: function() {
return m("input", {
oninput: m.withAttr("value", state.setValue),
value: state.value,
})
}
}
m.mount(document.body, Component)
m.render(document.body, m.trust("<h1>Hello</h1>"))
var count = 0
function inc() {
setInterval(function() {
count++
m.redraw()
}, 1000)
}
var Counter = {
oninit: inc,
view: function() {
return m("div", count)
}
}
m.mount(document.body, Counter)