Recipe-Box Recipe模塊

創(chuàng)建 images 目錄酣胀,并放置一張圖片當(dāng)做默認(rèn)圖片,最好把 images 目錄權(quán)限設(shè)置為 777(images的路徑: public)


20180105174650.png

修改 路由文件 index.js (路徑:resources/assets/js/router 下)

import Vue from 'vue';
import VueRouter from 'vue-router';

import Register from '../views/Auth/Register.vue';
import Login from '../views/Auth/Login.vue';

import RecipeIndex from '../views/Recipes/Index.vue';
import RecipeShow from '../views/Recipes/Show.vue';
import RecipeForm from '../views/Recipes/Form.vue';

Vue.use(VueRouter);

const router = new VueRouter({
    routes: [
        {path: '/register', component: Register},
        {path: '/login', component: Login},
        {path: '/', component: RecipeIndex},
        {path: '/recipes/create', component: RecipeForm, meta: {mode: 'create'}},
        {path: '/recipes/:id/edit', component: RecipeForm, meta: {mode: 'edit'}},
        {path: '/recipes/:id', component: RecipeShow}
    ]
});

export default router;

添加 Recipes 文件夾 并在該文件夾下創(chuàng)建 Index.vue 全度、Form.vue 和 Show.vue (創(chuàng)建Recipes文件夾 的 路徑:/resources/assets/js/views)

Index.vue

<template>
    <div class="recipe__list">
        <div class="recipe__item" v-for="recipe in recipes">
            <router-link class="recipe__inner" :to="`/recipes/${recipe.id}`">
                <img :src="`/images/${recipe.image}`" v-if="recipe.image">
                <p class="recipe__name">{{recipe.name}}</p>
            </router-link>
        </div>
    </div>
</template>

<script type="text/javascript">
    import { get } from '../../helpers/api';
    export default{
        data(){
            return {
                recipes : []
            }
        },
        methods: {
        },
        created(){
            get('/api/recipes')
                .then((res) => {
                    this.recipes = res.data.recipes;
                })
                .catch((err) => {
                });
        }
    }
</script>

Form.vue

<template>
    <div class="recipe__show">

        <div class="recipe__header">
            <h3>{{action}} Recipe</h3>
            <div>
                <button class="btn btn__primary" @click="save" :disabled="isProcessing">Save</button>
                <button class="btn" @click="$router.back()" :disabled="isProcessing">Cancel</button>
            </div>
        </div>

        <div class="recipe__row">
            <div class="recipe__image">
                <div class="recipe__box">
                    <image-upload v-model="form.image"></image-upload>
                    <small class="error__control" v-if="error.image">{{error.image[0]}}</small>
                </div>
            </div>

            <div class="recipe__details" >
                <div class="recipe__details_inner">
                    <div class="form__group">
                        <label>Name</label>
                        <input type="text" class="form__control" v-model="form.name">
                        <small class="error__control" v-if="error.name">{{error.name[0]}}</small>
                    </div>

                    <div class="form__group">
                        <label>Description</label>
                        <textarea type="text" class="form__control" v-model="form.description"></textarea>
                        <small class="error__control" v-if="error.description">{{error.description[0]}}</small>
                    </div>

                </div>
            </div>
        </div>

        <div class="recipe__row">
            <div class="recipe__ingredients">
                <div class="recipe__box">
                    <h3 class="recipe__sub_title">Ingredients</h3>
                    <div v-for="(ingredient, index) in form.ingredients" class="recipe__form">
                        <input type="text" class="form__control" v-model="ingredient.name"
                               :class="[error[`ingredients.${index}.name`] ? 'error__bg' : '']">

                        <input type="text" class="form__control form__qty" v-model="ingredient.qty"
                               :class="[error[`ingredients.${index}.qty`] ? 'error__bg' : '']">

                        <button class="btn btn__danger" @click="remove('ingredients', index)">
                            &times;
                        </button>
                    </div>

                    <button class="btn" @click="addIngredient">Add Ingredient</button>
                </div>
            </div>

            <div class="recipe__directions">
                <div class="recipe__directions_inner">
                    <h3 class="recipe__sub_title">Directions</h3>
                    <div v-for="(direction, index) in form.directions" class="recipe__form">
                    <textarea type="text" class="form__control" v-model="direction.description"
                              :class="[error[`directions.${index}.description`] ? 'error__bg' : '']"></textarea>

                        <button class="btn btn__danger" @click="remove('directions', index)">
                            &times;
                        </button>
                    </div>

                    <button class="btn" @click="addDirection">Add Direction</button>
                </div>
            </div>
        </div>

    </div>
</template>

<script type="text/javascript">
    import Vue from 'vue';
    import Flash from '../../helpers/flash';
    import { get, post } from '../../helpers/api';
    import { toMulipartedForm } from '../../helpers/form';
    import ImageUpload from '../../components/ImageUpload.vue';
    export default{
        data(){
            return {
                form: {
                    ingredients: [],
                    directions: []
                },
                error: {},
                isProcessing: false,
                initializeURL: `/api/recipes/create`,
                storeURL: `/api/recipes`,
                action: 'Create'
            }
        },
        components:{
            ImageUpload
        },
        methods: {
            save(){
                this.isProcessing = true;
                const form = toMulipartedForm(this.form, this.$route.meta.mode);
                post(this.storeURL, form)
                    .then((res) => {
                        if(res.data.saved){
                            Flash.setSuccess(res.data.message);
                            this.$router.push(`/recipes/${res.data.id}`);
                        }
                    }).catch((err) => {
                        if(err.response.status === 422){
                            this.error = err.response.data;
                        }
                        this.isProcessing = false;
                    })
            },
            addDirection(){
                this.form.directions.push({description: ''});
            },
            addIngredient(){
                this.form.ingredients.push({
                    name: '',
                    qty: ''
                });
            },
            remove(type, index){
                if(this.form[type].length > 1){
                    this.form[type].splice(index, 1);
                }
            }
        },
        created(){
            if(this.$route.meta.mode === 'edit') {
                this.initializeURL = `/api/recipes/${this.$route.params.id}/edit`;
                this.storeURL = `/api/recipes/${this.$route.params.id}?_method=PUT`;
                this.action = 'Update';
            }
            get(this.initializeURL).then((res) => {
                Vue.set(this.$data, 'form', res.data.form);
            }).catch((err) => {
                console.log(err);
            })
        }
    }
</script>

Show.vue

<template>
    <div class="recipe__show">
        <div class="recipe__row">
            <div class="recipe__image">
                <div class="recipe__box">
                    <img :src="`/images/${recipe.image}`" v-if="recipe.image" width="340px;">
                </div>
            </div>

            <div class="recipe__details">
                <div class="recipe__details_inner">
                    <small>Submitted by: {{recipe.user.name}}</small>
                    <h1 class="recipe__title">{{recipe.name}}</h1>
                    <p class="recipe__description">{{recipe.description}}</p>
                    <div v-if="auth.api_token && auth.user_id === recipe.user_id">
                        <router-link :to="`/recipes/${recipe.id}/edit`" class="btn btn-primary">
                            Edit
                        </router-link>

                        <button class="btn btn__danger" @click="remove" :disabled="isRemoving">Delete</button>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="recipe__row">
            <div class="recipe__ingredients">
                <div class="recipe__box">
                    <h3 class="recipe__sub_title">Ingredients</h3>
                    <ul>
                        <li v-for="ingredient in recipe.ingredients">
                            <span>{{ingredient.name}}</span>
                            <span>{{ingredient.qty}}</span>
                        </li>
                    </ul>
                </div>
            </div>

            <div class="recipe__directions">
                <div class="recipe__directions_inner">
                    <h3 class="recipe__sub_title">Directions</h3>
                    <ul>
                        <li v-for="(direction, i) in recipe.directions">
                            <p>
                                <strong>{{i + 1}}</strong>
                                {{direction.description}}
                            </p>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</template>

<script type="text/javascript">
    import Auth from '../../store/auth';
    import Flash from '../../helpers/flash';
    import { get, del} from '../../helpers/api';
    export default{
        data(){
            return {
                auth: Auth.state,
                isRemoving: false,
                recipe: {
                    user: {},
                    ingredients: [],
                    directions: []
                }
            }
        },
        methods: {
            remove(){
                this.isRemoving = false;
                del(`/api/recipes/${this.$route.params.id}`)
                    .then((res) =>{
                        if (res.data.deleted){
                            Flash.setSuccess('刪除操作成功煮剧!');
                            this.$router.push('/');
                        }
                    }).catch((err) => {
                });
            }
        },
        created(){
            console.log(this.$route.params);
            get(`/api/recipes/${this.$route.params.id}`)
                .then((res) =>{
                    this.recipe = res.data.recipe;
                }).catch((err) => {
            })
        }
    }
</script>

在 components 文件夾下創(chuàng)建 ImagePreview.vue 和 ImageUpload.vue(路徑:/resources/assets/js/components/)

ImagePreview.vue

<template>
    <div class="image__preview" v-if="image">
        <img :src="image">
        <button class="btn btn__danger image__close" @click="$emit('close')">&times;</button>
    </div>
</template>

<script type="text/javascript">
    export default{
        data(){
            return {
                image: null
            }
        },
        props: {
            preview: {
                type: [String, File],
                default: null
            }
        },
        watch: {
            'preview': 'setPreview'
        },
        methods: {
            setPreview(){
                if(this.preview instanceof File){
                    const fileReader = new FileReader;
                    fileReader.onload = (event) => {
                        this.image = event.target.result;
                    };
                    fileReader.readAsDataURL(this.preview);
                }else if(typeof this.preview === 'string'){
                    this.image = `images/${this.preview}`;
                }else {
                    this.image = null;
                }
            }
        },
        created(){
            this.setPreview();
        }
    }
</script>

ImageUpload.vue

<template>
    <div class="image">
        <image-preview :preview="value" @close="$emit('input', null)" v-if="value"></image-preview>
        <div class="image__upload" v-else>
            <input type="file" accept="images/*" @change="upload">
        </div>
    </div>
</template>

<script type="text/javascript">
    import ImagePreview from './ImagePreview.vue';
    export default{
        props: {
            value: {
                type: [String, File],
                default: null
            }
        },
        components: {
            ImagePreview
        },
        methods: {
            upload(e){
                const files = e.target.files;
                console.log(files);
                if(files && files.length > 0){
                    this.$emit('input', files[0]);
                }
            }
        }
    }
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市将鸵,隨后出現(xiàn)的幾起案子勉盅,更是在濱河造成了極大的恐慌,老刑警劉巖顶掉,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件草娜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡痒筒,警方通過(guò)查閱死者的電腦和手機(jī)宰闰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)簿透,“玉大人移袍,你說(shuō)我怎么就攤上這事±铣洌” “怎么了葡盗?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)啡浊。 經(jīng)常有香客問(wèn)我觅够,道長(zhǎng),這世上最難降的妖魔是什么巷嚣? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任喘先,我火速辦了婚禮,結(jié)果婚禮上廷粒,老公的妹妹穿的比我還像新娘窘拯。我一直安慰自己,他們只是感情好评雌,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布树枫。 她就那樣靜靜地躺著,像睡著了一般景东。 火紅的嫁衣襯著肌膚如雪砂轻。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,562評(píng)論 1 305
  • 那天斤吐,我揣著相機(jī)與錄音搔涝,去河邊找鬼厨喂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛庄呈,可吹牛的內(nèi)容都是我干的蜕煌。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼诬留,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼斜纪!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起文兑,我...
    開(kāi)封第一講書(shū)人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤盒刚,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后绿贞,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體因块,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年籍铁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涡上。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拒名,死狀恐怖吩愧,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情靡狞,我是刑警寧澤耻警,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站甸怕,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏腮恩。R本人自食惡果不足惜梢杭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望秸滴。 院中可真熱鬧武契,春花似錦、人聲如沸荡含。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)释液。三九已至全释,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間误债,已是汗流浹背浸船。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工妄迁, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人李命。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓登淘,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親封字。 傳聞我的和親對(duì)象是個(gè)殘疾皇子黔州,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 修改 welcome.blade.php 文件 (路徑:resources/views/welcome.blade...
    三仕貳號(hào)閱讀 404評(píng)論 0 0
  • jHipster - 微服務(wù)搭建 CC_簡(jiǎn)書(shū)[http://www.reibang.com/u/be0d56c4...
    quanjj閱讀 812評(píng)論 0 2
  • 本文基于工作項(xiàng)目開(kāi)發(fā),做的整理筆記因工作需要阔籽,項(xiàng)目框架由最初的Java/jsp模式流妻,逐漸轉(zhuǎn)移成node/expre...
    SeasonDe閱讀 7,446評(píng)論 3 35
  • 響應(yīng)式布局的理解 響應(yīng)式開(kāi)發(fā)目的是一套代碼可以在多種終端運(yùn)行,適應(yīng)不同屏幕的大小,其原理是運(yùn)用媒體查詢,在不同屏幕...
    懶貓_6500閱讀 787評(píng)論 0 0
  • 去上村得走兩里地,要穿過(guò)長(zhǎng)一段無(wú)人的山路仿耽,路過(guò)一座石橋合冀,一座小石廟,爬一條長(zhǎng)坡和一片古老秘林项贺。對(duì)于當(dāng)時(shí)小小的而言君躺,...
    李子香香閱讀 342評(píng)論 0 0