1. 程式人生 > >semantic-ui-react圖像組件不顯示圖像的問題

semantic-ui-react圖像組件不顯示圖像的問題

semantic-ui-react圖像

問題

React提供了一種相當科學的開發Web應用前端視圖層的技術,借助於semantic-ui for React可以很容易地搭建起常用網頁開發中的UI組件,但是最近在分析其提供的各種組件時發現Image組件存在一個小問題,記錄於此。

我最初代碼形式

import React from ‘react‘
import {
    Grid,
    Image,
} from ‘semantic-ui-react‘

const ImageExampleLink = () => (
    <Grid container stackable verticalAlign=‘middle‘>
        <Grid.Row>
            <Grid.Column  floated=‘left‘ width={4}>
                <Image
                    bordered
                    rounded
                    size=‘small‘
                    src=‘./help.png‘
                />
            </Grid.Column>
            <Grid.Column floated=‘right‘ width={4}>
                <Image
                    bordered
                    rounded
                    size=‘small‘
                    src=‘./help.png‘
                />
            </Grid.Column>
        </Grid.Row>
        <Grid.Row>
            <Grid.Column  floated=‘right‘ >
                <Image
                    size=‘mini‘
                    avatar
                    src=‘./help.png‘
                />
            </Grid.Column>
        </Grid.Row>
    </Grid>
)

export default ImageExampleLink

其中圖像文件的路徑位置沒有問題,但是運行中發現圖像死活不顯示,只顯示一個通用的系統提供的那種你常見的占位符形式。
然後,通過google瀏覽器搜索分析,發現了一個看似相近的題目「Semantic-UI Image properties not working with semantic-ui-react」(https://stackoverflow.com/questions/44609711/semantic-ui-image-properties-not-working-with-semantic-ui-react)。
其實,上面的問題與我的不一樣,semantic-ui-react系統所使用的css文件我是的確導入了。其中,那位回答者的回答是“Semantic UI React requires a Semantic UI‘ CSS, you forgot to setup it, here is instuctions.”。我跟蹤這個鏈接過去,只是簡單地轉入了官方展示網站,沒有什麽,我早就比較熟悉這個地方了。但是,受到此處的一點啟動,我把代碼中的src屬性的地址形式變換了一個,使用了JSX語言推薦的如下表達形式。

改進後代碼形式

import React from ‘react‘
import {
    Grid,
    Image,
} from ‘semantic-ui-react‘
import help from ‘./help.png‘;

const ImageExampleLink = () => (
    <Grid container stackable verticalAlign=‘middle‘>
        <Grid.Row>
            <Grid.Column  floated=‘left‘ width={4}>
                <Image
                    bordered
                    rounded
                    size=‘small‘
                    src={help}
                />
            </Grid.Column>
            <Grid.Column floated=‘right‘ width={4}>
                <Image
                    bordered
                    rounded
                    size=‘small‘
                    src={help}
                />
            </Grid.Column>
        </Grid.Row>
        <Grid.Row>
            <Grid.Column  floated=‘right‘ >
                <Image
                    size=‘mini‘
                    avatar
                    src={help}
                />
            </Grid.Column>
        </Grid.Row>
    </Grid>
)

export default ImageExampleLink

請註意上面代碼最初的import語句和改進後的Image組件的src屬性值的表達方式。再試運行,OK!

有關這種原因的深層官方解決仍然在探索中,有此問題的,可暫時參考一下。

semantic-ui-react圖像組件不顯示圖像的問題