标签:npm node 求和 The and res 子集 dump resource
Staticfile CDN(国内):
unpkg:会保持和npm发布的最新的版本一致
cdnjs:
在new Vue设置:
`delimiters:['[[', ']]']`,
然后在html使用 `[[ ]]` 代替 `{{}}`
使用
`{% verbatim myblock %} {% endverbatim myblock %}`
包裹vue里面的`{{}}`,
此标签包裹的代码将不会被Django的模板引擎渲染
django服务端代码:
def vue_views(request):
List = [{'age':18},200]
OBJ = {"name":"隔壁老王"}
dic = {
'list':json.dumps(List),
'obj':json.dumps(OBJ)
}
return render(request,'Vue.html',dic)
Vue前端代码:
<script>
var list = {{ list | safe }}
var obj = {{ obj | safe }}
console.log(list,obj)
</script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
axios.get('/newmodle?a=1&b=2')
.then(function (response) {
console.log(response)
console.log(response.data)
})
</script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script>
<script>
function getCookie (name) {
var value = '; ' + document.cookie
var parts = value.split('; ' + name + '=')
if (parts.length === 2) return parts.pop().split(';').shift()
}
axios({
url: '/newmodle/',
method: 'post',
responseType: 'json', // 默认的
data: Qs.stringify({
'a': 1,
'b': 2,
}),
headers: {
'X-CSRFToken': this.getCookie('csrftoken')
}
})
.then(function (response) {
console.log(response)
console.log(response.data)
})
</script>
Vue 要实现异步加载需要使用到 vue-resource 库
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
this.$http.get('/user/newmodle?a=1&b=2')
.then(function(response){
console.log(response);
console.log(response.data)
}
<script src="/static/vue.min.js"></script>
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
<script src="https://cdn.bootcss.com/qs/6.5.1/qs.min.js"></script>
<script>
function getCookie (name) {
var value = '; ' + document.cookie
var parts = value.split('; ' + name + '=')
if (parts.length === 2) return parts.pop().split(';').shift()
}
var app = new Vue({
el : '#app',
data : {
text:''
},
mounted:function(){
this.$http({
url: '/newmodle',
method: 'post',
responseType: 'json',
data: Qs.stringify({
'a': 1,
'b': 2,
}),
headers: {'X-CSRFToken': getCookie('csrftoken')}
})
.then(function (response) {
console.log(response);
console.log(response.data);
})
}
})
</script>
Ajax
Ajax 即“Asynchronous Javascript And XML”,是指一种创建交互式网页应用的网页开发技术
axios:
用于浏览器和node.js的基于Promise的HTTP客户端
学习随笔:Vue.js与Django交互以及Ajax和axios
标签:npm node 求和 The and res 子集 dump resource
原文地址:https://www.cnblogs.com/wangbaby/p/10538227.html