鍍金池/ 問答/HTML/ vue函數(shù)式組件中的render函數(shù)如何添加偽類樣式

vue函數(shù)式組件中的render函數(shù)如何添加偽類樣式

問題描述

export default {
    functional: true,
    props: {
        data: {
            type: String,
            default() {
                return '';
            }
        },
        depth: {
            type: Number
        }
    },
    render: (h, ctx) => {
        return h('div', {
            class: ['treeitem'],
            style: {
                ':hover': {
                    background: 'yellow'  // 希望在div這個(gè)元素上添加一個(gè)偽類的樣式,但是沒有效果
                },
                height: '40px',
                lineHeight: '40px',
                border: '1px solid #f0f0f0',
                'marginLeft': ctx.props.depth*20 + 'px'
            },
            on: {
                click: () => {
                },
                mouseover: () => {
                }
            }
        }, [
            h('Icon', {
                props: {
                    type: 'arrow-right-b',
                },
                style: {
                    marginLeft: '20px',
                    display: 'inline-block',
                    width: '40px',
                    hight: '40px'
                }
            }),
            h('span',{
                style: {
                }
            }, ctx.props.data),
            h('Button', {
                props: {
                    icon: 'ios-gear-outline'
                },
                style: {
                    float: 'right' 
                },
                on: {
                    click: () => {
                    }
                }
            })
        ]);
    }
}

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

嘗試為div元素添加class,但是作為函數(shù)式組件,這個(gè)是js文件,style該寫在哪里?

相關(guān)代碼

// 請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

回答
編輯回答
妖妖

推薦綁定到class上

2018年8月7日 07:55
編輯回答
祉小皓

推薦通過class來操作,然后通過class選中偽類

2017年7月8日 02:24
編輯回答
任她鬧

這不是 vue 的問題,目前行內(nèi)樣式是不支持寫偽類的

可以嘗試在 head 中添加一個(gè) style 標(biāo)簽

const styleEl = document.createElement('style')
styleEl.innerHTML = `
  .treeitem:hover{
    background: yellow;
  }
`
document.head.appendChild(styleEl)
2017年5月10日 16:02