溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

在pycharm中開發(fā)vue的方法步驟

發(fā)布時間:2020-09-23 13:33:19 來源:腳本之家 閱讀:795 作者:Zhuang_Z 欄目:web開發(fā)

一.在pycharm中開發(fā)vue

'''
webstorm(vue) pycharm(python) goland(Go語言) idea(java) andrioStuidio(安卓) Php(PHP)
'''

'''
①在pycharm中打開vue項目,在settins下Plugins中下載vue.js
②啟動vue項目
 -方法1.在Terminal下輸入npm run serve
 -方法2.Edit Configurations----》點(diǎn)+ 選npm-----》在script對應(yīng)的框中寫:serve
'''

二.vue項目的目錄結(jié)構(gòu)

-node_modules:項目的依賴

-public
  -favicon.ico 網(wǎng)頁的圖標(biāo)
  -index.html  主頁面
-src:我們需要關(guān)注的
  -assets:方靜態(tài)文件
  -components:小組件
  -views :頁面組件
  -App.vue :主組件
  -main.js :項目主入口js
  -router.js: 路由相關(guān),以后配置路由,都在這里配置
  -store.js :vuex相關(guān),狀態(tài)管理器

-package.json  項目的依賴文件

三.每個vue組件由三部分組成

  • template:放html代碼
  • script:放js相關(guān)的東西
  • style:放css相關(guān)

四.vue中路由的創(chuàng)建

①在src下views文件夾中創(chuàng)建一個組件 FreeCourse.vue

②配置路由

在src下router.js中配置

  import FreeCourse from './views/FreeCourse.vue'
  
  {
   path: '/freecourse',
   name: 'freecourse',
   component: FreeCourse
  },

③路由跳轉(zhuǎn)

在src下APP.vue中配置

<router-link to="/freecourse">免費(fèi)課程</router-link>

五.在組件中顯示數(shù)據(jù)

①在template中:

<div class="course">
  {{course_list}}
</div>

②在script中:

export default {
 name: 'course',
 data: function () {
   return{
    course_list:['python','linux','go語言']
   }
 }
}

六.vue中的axios完成前后臺交互

-安裝

npm install axios 在package.json文件中就能看到依賴

-在main.js中配置

  //導(dǎo)入 axios
  import axios from 'axios'
  //把a(bǔ)xios對象賦給$http
  Vue.prototype.$http=axios
  //以后在組件的js中通過$http取到的就是axios

-在組件的js代碼中寫:

  this.$http.request({
    //向下面的地址發(fā)送get請求
    url:'http://127.0.0.1:8000/courses/',
    method:'get'
  }).then(function (response) {
    //response.data才是真正的數(shù)據(jù)
    console.log(response.data)
  })

-頁面掛載完成,執(zhí)行后面函數(shù),完成數(shù)據(jù)加載

  mounted:function () {
    this.init()
  }
    

組件

<template>
 <div class="course">
  <h2>我是免費(fèi)課程頁面</h2>
  <p v-for="course in course_list">{{course}}</p>
 </div>
</template>

<script>


export default {
 name: 'course',
 data: function () {
   return{
    course_list:[]
   }
 },
 methods: {
   'init':function () {
     var _this = this;
     this.$http.request({
       //向下面的地址發(fā)送get請求
       url:'http://127.0.0.1:8000/courses/',
       method:'get'
     }).then(function (response) {
       //response.data才是真正的數(shù)據(jù)
       _this.course_list = response.data
     })
   }
 } ,
 mounted:function () {
   this.init()
 }
}
</script>

七.vue中使用element-ui

-餓了么開源樣式

-安裝 npm i element-ui -S

-在main.js中配置

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

-去官方文檔看樣式完成復(fù)制粘貼 http://element-cn.eleme.io/#/zh-CN

八.contentype組件(數(shù)據(jù)庫相關(guān))

什么時候使用?

實(shí)際項目中有一個表(PricePolicy)要關(guān)聯(lián)好幾個表(Course,DegreeCourse)也就是這個表要儲存好幾個表的數(shù)據(jù),這種情況使用contentype組件

-新建免費(fèi)課程表的時候 Course

# 不會在數(shù)據(jù)庫中生成字段,只用于數(shù)據(jù)庫操作
policy = GenericRelation(to='PricePolicy')

-新建學(xué)位課程表的時候 DegreeCourse

# 不會在數(shù)據(jù)庫中生成字段,只用于數(shù)據(jù)庫操作
policy = GenericRelation(to='PricePolicy')

-價格策略表 PricePolicy

#之前有的字段該怎么寫就怎么寫
object_id = models.IntegerField()
content_type = models.ForeignKey(to=ContenType,null=True)
# 引入一個字段,不會在數(shù)據(jù)庫中創(chuàng)建,只用來做數(shù)據(jù)庫操作
content_obj = GenericForeignKey()

使用一(給課程添加價格策略):

-給免費(fèi)課django添加價格策略

course = models.Course.objects.get(pk=1)
ret=models.PricePolicy.objects.create(period=30, price=199.9,content_obj=course)

-給學(xué)位課程(python全棧開發(fā))添加價格策略

degree_course = models.DegreeCourse.objects.get(pk=1)
ret=models.PricePolicy.objects.create(period=30, price=199.9,content_obj=degree_course)

使用二:查詢價格策略對應(yīng)的課程:

price_policy=models.PricePolicy.objects.get(pk=1)
print(price_policy.content_obj)

使用三:通過課程獲取價格策略

course = models.Course.objects.get(pk=1)
policy_list=course.policy.all()

到此這篇關(guān)于在pycharm中開發(fā)vue的方法步驟的文章就介紹到這了,更多相關(guān)pycharm開發(fā)vue內(nèi)容請搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI